package fn.compose.theme

import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable

/**
 * Material 3 [androidx.compose.material3.ColorScheme] derived from [FnColors].
 *
 * This is the registry's canonical dark identity (Mantine v9 dark + indigo).
 * Equivalent to `FnMantineProvider` (web) and `fn::run_app` with
 * `ThemeMode::FnDark` (C++).
 */
private val FnDarkColorScheme = darkColorScheme(
    primary = FnColors.primary,
    onPrimary = FnColors.white,
    primaryContainer = FnColors.primaryActive,
    onPrimaryContainer = FnColors.white,
    secondary = FnColors.primaryLight,
    onSecondary = FnColors.white,
    background = FnColors.bg,
    onBackground = FnColors.text,
    surface = FnColors.surface,
    onSurface = FnColors.text,
    surfaceVariant = FnColors.surfaceHover,
    onSurfaceVariant = FnColors.textMuted,
    error = FnColors.error,
    onError = FnColors.white,
    outline = FnColors.border,
    outlineVariant = FnColors.borderStrong,
)

private val FnLightColorScheme = lightColorScheme(
    primary = FnColors.primary,
    onPrimary = FnColors.white,
    primaryContainer = FnColors.primaryLight,
    onPrimaryContainer = FnColors.white,
    secondary = FnColors.primaryActive,
    onSecondary = FnColors.white,
    background = FnColors.lightBg,
    onBackground = FnColors.lightText,
    surface = FnColors.lightSurface,
    onSurface = FnColors.lightText,
    surfaceVariant = FnColors.lightSurface,
    onSurfaceVariant = FnColors.lightTextMuted,
    error = FnColors.error,
    onError = FnColors.white,
    outline = FnColors.lightBorder,
)

/**
 * Root theme provider for every registry Android app. Dark by default, mirroring
 * the web frontend's `defaultColorScheme="dark"`.
 *
 * Usage:
 * ```
 * setContent {
 *     FnTheme {
 *         Surface(Modifier.fillMaxSize()) { /* ... */ }
 *     }
 * }
 * ```
 *
 * Override the scheme with `FnTheme(darkMode = false) { ... }` or follow the
 * system with `FnTheme(darkMode = isSystemInDarkTheme()) { ... }`.
 *
 * Apps must NOT call `MaterialTheme {}` directly — always go through `FnTheme`.
 */
@Composable
fun FnTheme(
    darkMode: Boolean = true,
    content: @Composable () -> Unit,
) {
    MaterialTheme(
        colorScheme = if (darkMode) FnDarkColorScheme else FnLightColorScheme,
        content = content,
    )
}
