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

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.ulfrx.recipe.ui.components.calendar.SwipeableCalendar
import dev.ulfrx.recipe.ui.components.calendar.todayInSystemTz
import dev.ulfrx.recipe.ui.theme.RecipeTheme
import org.jetbrains.compose.resources.stringResource
import recipe.composeapp.generated.resources.Res
import recipe.composeapp.generated.resources.shell_tab_planner

/**
 * Phase 2.1 — planner shell with the shared calendar at the top. Phase 6 fills
 * in the area below the calendar with meal slots driven by [PlannerState.selectedDate].
 *
 * Search is shell-wide; this screen owns no bottom-chrome state.
 */
@Composable
fun PlannerScreen(viewModel: PlannerViewModel) {
    val state by viewModel.state.collectAsStateWithLifecycle()
    val today = remember { todayInSystemTz() }

    Box(
        modifier =
            Modifier
                .fillMaxSize()
                .background(RecipeTheme.colors.background),
    ) {
        Column(
            modifier =
                Modifier
                    .fillMaxSize()
                    .windowInsetsPadding(WindowInsets.statusBars),
        ) {
            BasicText(
                text = stringResource(Res.string.shell_tab_planner),
                style =
                    RecipeTheme.typography.title.copy(
                        color = RecipeTheme.colors.content,
                    ),
                modifier =
                    Modifier.padding(
                        top = RecipeTheme.spacing.xl,
                        start = RecipeTheme.spacing.lg,
                        end = RecipeTheme.spacing.lg,
                    ),
            )

            Spacer(modifier = Modifier.height(RecipeTheme.spacing.lg))

            SwipeableCalendar(
                selectedDate = state.selectedDate,
                today = today,
                mode = state.calendarMode,
                onSelectDate = viewModel::selectDate,
                onModeChange = viewModel::setCalendarMode,
                // Swipe auto-follows: dropping into a new week/month bumps
                // the selection by the same offset (kotlinx.datetime clamps
                // day-of-month for short months).
                onVisibleAnchorChange = viewModel::selectDate,
                expandable = true,
                modifier = Modifier.fillMaxWidth(),
            )
        }
    }
}
