package app.closer.ui.questions

import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import app.closer.core.navigation.AppRoute
import app.closer.domain.model.Question

@Composable
fun DailyQuestionScreen(
    onNavigate: (String) -> Unit = {},
    viewModel: DailyQuestionViewModel = hiltViewModel()
) {
    val state by viewModel.uiState.collectAsState()

    LocalQuestionContent(
        state = state,
        title = "One question, enough space",
        subtitle = "Answer privately first, then choose whether to reveal it or keep the conversation going.",
        primaryRouteLabel = "Discuss",
        onPrimaryRoute = { question ->
            val coupleId = state.coupleId
            if (coupleId != null) {
                onNavigate(AppRoute.questionThread(coupleId, question.id))
            } else {
                // Discussing requires a paired partner; send unpaired users to invite one.
                onNavigate(AppRoute.CREATE_INVITE)
            }
        },
        onSecondaryRoute = state.question?.let {
            { onNavigate(AppRoute.answerReveal(it.id)) }
        },
        secondaryRouteLabel = "Reveal",
        onWrittenTextChanged = viewModel::updateWrittenText,
        onOptionToggled = viewModel::toggleOption,
        onScaleChanged = viewModel::updateScale,
        onSubmit = viewModel::submitAnswer,
        canSubmit = viewModel.canSubmit(),
        onRefresh = viewModel::loadDailyQuestion
    )
}

@Preview
@Composable
fun DailyQuestionScreenPreview() {
    LocalQuestionContent(
        state = LocalQuestionUiState(
            isLoading = false,
            question = Question(
                id = "demo",
                text = "What is one small thing that would help us feel close tonight?",
                category = "emotional_intimacy",
                depthLevel = 2,
                type = "written"
            )
        ),
        title = "One question, enough space",
        subtitle = "Answer privately first, then choose what happens next.",
        primaryRouteLabel = "Discuss",
        onPrimaryRoute = {},
        onSecondaryRoute = {},
        secondaryRouteLabel = "Reveal",
        onWrittenTextChanged = {},
        onOptionToggled = {},
        onScaleChanged = {},
        onSubmit = {},
        canSubmit = false
    )
}
