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 = {},
    onBack: () -> Unit = {},
    viewModel: DailyQuestionViewModel = hiltViewModel()
) {
    val state by viewModel.uiState.collectAsState()

    // The reveal route is available whenever both answered, and remains available after
    // reveal so users can always navigate back to see both answers (labeled "View reveal").
    val revealAvailable = state.submitted && (state.partnerHasAnswered || state.isRevealed)
    val revealRoute: (() -> Unit)? = if (revealAvailable) {
        state.question?.let { q -> { onNavigate(AppRoute.answerReveal(q.id)) } }
    } else null
    val revealLabel = when {
        state.isRevealed -> "View reveal"
        revealRoute != null -> "Reveal"
        else -> null
    }

    val mode = state.dailyMode
    val title = mode?.title ?: "One question, enough space"
    val subtitle = mode?.subtitle ?: "Answer privately first, then choose whether to reveal it or keep the conversation going."

    LocalQuestionContent(
        state = state,
        title = title,
        subtitle = subtitle,
        primaryRouteLabel = "Discuss",
        onPrimaryRoute = { question ->
            val coupleId = state.coupleId
            if (coupleId != null) {
                onNavigate(AppRoute.conversation(coupleId, "q_" + question.id))
            } else {
                onNavigate(AppRoute.CREATE_INVITE)
            }
        },
        onSecondaryRoute = revealRoute,
        secondaryRouteLabel = revealLabel,
        onWrittenTextChanged = viewModel::updateWrittenText,
        onOptionToggled = viewModel::toggleOption,
        onScaleChanged = viewModel::updateScale,
        onSubmit = viewModel::submitAnswer,
        canSubmit = viewModel.canSubmit(),
        onBack = onBack
    )
}

@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
    )
}
