package com.fabledsword.minstrel.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider

/**
 * The Flutter theme's `ColorScheme` role mapping (theme_data.dart):
 *   primary           = accent (forest-teal) — same both modes
 *   onPrimary         = onAction
 *   surface           = iron
 *   onSurface         = parchment
 *   secondary         = moss
 *   error             = error
 *   scaffoldBackground = obsidian
 *
 * Reproduced here for both modes. Note that token names ARE semantic
 * roles — `iron` is "raised surface" in both modes (#1E2228 dark vs
 * #ECE6D5 light), not literal "near-black".
 */
private fun darkMaterialColorScheme() =
    darkColorScheme(
        primary = FabledSwordFlatTokens.accent,
        onPrimary = FabledSwordFlatTokens.onAction,
        primaryContainer = FabledSwordFlatTokens.accent.copy(alpha = 0.25f),
        onPrimaryContainer = FabledSwordDarkTokens.parchment,
        secondary = FabledSwordFlatTokens.moss,
        onSecondary = FabledSwordFlatTokens.onAction,
        background = FabledSwordDarkTokens.obsidian,
        onBackground = FabledSwordDarkTokens.parchment,
        surface = FabledSwordDarkTokens.iron,
        onSurface = FabledSwordDarkTokens.parchment,
        surfaceVariant = FabledSwordDarkTokens.slate,
        onSurfaceVariant = FabledSwordDarkTokens.ash,
        error = FabledSwordFlatTokens.error,
        onError = FabledSwordFlatTokens.onAction,
        outline = FabledSwordDarkTokens.pewter,
        outlineVariant = FabledSwordDarkTokens.slate,
    )

private fun lightMaterialColorScheme() =
    lightColorScheme(
        primary = FabledSwordFlatTokens.accent,
        onPrimary = FabledSwordFlatTokens.onAction,
        primaryContainer = FabledSwordFlatTokens.accent.copy(alpha = 0.18f),
        onPrimaryContainer = FabledSwordLightTokens.parchment,
        secondary = FabledSwordFlatTokens.moss,
        onSecondary = FabledSwordFlatTokens.onAction,
        background = FabledSwordLightTokens.obsidian,
        onBackground = FabledSwordLightTokens.parchment,
        surface = FabledSwordLightTokens.iron,
        onSurface = FabledSwordLightTokens.parchment,
        surfaceVariant = FabledSwordLightTokens.slate,
        onSurfaceVariant = FabledSwordLightTokens.ash,
        error = FabledSwordFlatTokens.error,
        onError = FabledSwordFlatTokens.onAction,
        outline = FabledSwordLightTokens.pewter,
        outlineVariant = FabledSwordLightTokens.slate,
    )

/**
 * Theme entry point. Selects dark vs light based on system mode and
 * provides both the Material 3 [MaterialTheme] and the
 * [LocalFabledSwordTheme] for token-level reads. [LocalActionColors]
 * is kept for back-compat — same values; consumers should migrate to
 * `LocalFabledSwordTheme.current.moss` etc. in a later cleanup.
 *
 * `darkOverride` lets callers force one mode (Phase 11 Settings will
 * carry a user-controlled override).
 */
@Composable
fun MinstrelTheme(
    darkOverride: Boolean? = null,
    content: @Composable () -> Unit,
) {
    val isDark = darkOverride ?: isSystemInDarkTheme()
    val colorScheme = if (isDark) darkMaterialColorScheme() else lightMaterialColorScheme()
    val fabledTheme = if (isDark) FabledSwordTheme.Dark else FabledSwordTheme.Light

    MaterialTheme(
        colorScheme = colorScheme,
        typography = MinstrelTypography,
    ) {
        CompositionLocalProvider(
            LocalFabledSwordTheme provides fabledTheme,
            LocalActionColors provides ActionColors(
                primary = FabledSwordFlatTokens.moss,
                secondary = FabledSwordFlatTokens.bronze,
                destructive = FabledSwordFlatTokens.oxblood,
                onAction = FabledSwordFlatTokens.onAction,
            ),
            content = content,
        )
    }
}
