package dev.ulfrx.recipe.ui.screens.auth

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeContentPadding
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.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.lokksmith.compose.rememberAuthFlowLauncher
import dev.ulfrx.recipe.auth.ComposeAuthBrowser
import dev.ulfrx.recipe.ui.components.controls.RecipePrimaryButton
import dev.ulfrx.recipe.ui.theme.RecipeTheme
import org.jetbrains.compose.resources.stringResource
import recipe.composeapp.generated.resources.Res
import recipe.composeapp.generated.resources.auth_app_name
import recipe.composeapp.generated.resources.auth_sign_in_button

/**
 * Visible during [dev.ulfrx.recipe.auth.AuthState.Unauthenticated]. Wordmark + sign-in
 * button + inline error text (when present). Inline-error UX rules and loading rules
 * locked in `02-UI-SPEC.md` § Copywriting Contract.
 */
@Composable
fun LoginScreen(viewModel: LoginViewModel) {
    val state by viewModel.state.collectAsStateWithLifecycle()
    val launcher = rememberAuthFlowLauncher()
    val browser = remember(launcher) { ComposeAuthBrowser(launcher) }

    Box(
        modifier =
            Modifier
                .fillMaxSize()
                .background(RecipeTheme.colors.surface),
    ) {
        Column(
            modifier =
                Modifier
                    .fillMaxSize()
                    .safeContentPadding()
                    .padding(horizontal = 16.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center,
        ) {
            BasicText(
                text = stringResource(Res.string.auth_app_name),
                style = RecipeTheme.typography.display.copy(color = RecipeTheme.colors.content),
            )
            Spacer(Modifier.height(24.dp))
            RecipePrimaryButton(
                text = stringResource(Res.string.auth_sign_in_button),
                onClick = { viewModel.onSignInClick(browser) },
                enabled = !state.isLoading,
                loading = state.isLoading,
            )
            val errorKey = state.errorKey
            if (errorKey != null) {
                Spacer(Modifier.height(16.dp))
                BasicText(
                    text = stringResource(errorKey),
                    style =
                        RecipeTheme.typography.body.copy(
                            color = RecipeTheme.colors.destructive,
                            textAlign = TextAlign.Center,
                        ),
                )
            }
        }
    }
}
