package dev.ulfrx.recipe.ui.screens.planner

import androidx.lifecycle.ViewModel
import dev.ulfrx.recipe.ui.components.calendar.todayInSystemTz
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.datetime.LocalDate

data class PlannerState(
    val selectedDate: LocalDate,
    val isCalendarOpen: Boolean = false,
)

class PlannerViewModel : ViewModel() {
    private val _state = MutableStateFlow(PlannerState(selectedDate = todayInSystemTz()))
    val state: StateFlow<PlannerState> = _state.asStateFlow()

    fun selectDate(date: LocalDate) {
        _state.update { it.copy(selectedDate = date) }
    }

    /**
     * Move the highlighted day without collapsing the calendar pill. Used by
     * the collapsed strip's week-paged swipe gesture so swipe-to-shift doesn't
     * also dismiss the calendar.
     */
    fun shiftSelection(date: LocalDate) {
        _state.update { it.copy(selectedDate = date) }
    }

    fun setCalendarOpen(open: Boolean) {
        _state.update { it.copy(isCalendarOpen = open) }
    }

    fun closeCalendar() {
        _state.update { it.copy(isCalendarOpen = false) }
    }
}
