package dev.ulfrx.recipe.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.remember

/**
 * Recipe theme entry point (CONTEXT D-14, D-15).
 *
 * All app UI reads `RecipeTheme.colors.*`, `RecipeTheme.typography.*`,
 * `RecipeTheme.spacing.*`, and local Recipe components built on Compose
 * Unstyled. Material 3 is deliberately absent from the composition.
 */
public val LocalRecipeColors: ProvidableCompositionLocal<RecipeColors> =
    androidx.compose.runtime.staticCompositionLocalOf { error("RecipeColors accessed outside RecipeTheme { }") }

public val LocalRecipeTypography: ProvidableCompositionLocal<RecipeTypography> =
    androidx.compose.runtime.staticCompositionLocalOf { error("RecipeTypography accessed outside RecipeTheme { }") }

public val LocalRecipeSpacing: ProvidableCompositionLocal<RecipeSpacing> =
    androidx.compose.runtime.staticCompositionLocalOf { error("RecipeSpacing accessed outside RecipeTheme { }") }

public val LocalRecipeShapes: ProvidableCompositionLocal<RecipeShapes> =
    androidx.compose.runtime.staticCompositionLocalOf { error("RecipeShapes accessed outside RecipeTheme { }") }

public val LocalRecipeGlass: ProvidableCompositionLocal<RecipeGlass> =
    androidx.compose.runtime.staticCompositionLocalOf { error("RecipeGlass accessed outside RecipeTheme { }") }

@Composable
public fun RecipeTheme(content: @Composable () -> Unit) {
    val dark = isSystemInDarkTheme()
    val recipeColors = if (dark) DarkRecipeColors else LightRecipeColors
    val recipeGlass = remember(recipeColors) { recipeGlassFor(recipeColors) }

    CompositionLocalProvider(
        LocalRecipeColors provides recipeColors,
        LocalRecipeTypography provides DefaultRecipeTypography,
        LocalRecipeSpacing provides DefaultRecipeSpacing,
        LocalRecipeShapes provides DefaultRecipeShapes,
        LocalRecipeGlass provides recipeGlass,
        content = content,
    )
}

object RecipeTheme {
    val colors: RecipeColors
        @Composable @ReadOnlyComposable
        get() = LocalRecipeColors.current

    val typography: RecipeTypography
        @Composable @ReadOnlyComposable
        get() = LocalRecipeTypography.current

    val spacing: RecipeSpacing
        @Composable @ReadOnlyComposable
        get() = LocalRecipeSpacing.current

    val shapes: RecipeShapes
        @Composable @ReadOnlyComposable
        get() = LocalRecipeShapes.current

    val glass: RecipeGlass
        @Composable @ReadOnlyComposable
        get() = LocalRecipeGlass.current
}
