import SwiftUI

// MARK: - Theme ID

enum ThemeID: String, CaseIterable, Codable {
    case linen, slate, mist, grove, ink, copper
    case abyss, dusk, basalt
    case chalk, flint
    case onyx, ember, birch, vapor

    var isPremium: Bool {
        switch self {
        case .linen, .slate, .mist, .chalk, .flint: return false
        default: return true
        }
    }

    var isDark: Bool {
        switch self {
        case .copper, .abyss, .dusk, .basalt, .flint, .onyx, .ember: return true
        default: return false
        }
    }

    var isNeurodiverseFocused: Bool {
        switch self {
        case .abyss, .dusk, .basalt: return true
        default: return false
        }
    }

    var displayName: String {
        switch self {
        case .linen:  return "Linen"
        case .slate:  return "Slate"
        case .mist:   return "Mist"
        case .grove:  return "Grove"
        case .ink:    return "Ink"
        case .copper: return "Copper"
        case .abyss:  return "Abyss"
        case .dusk:   return "Dusk"
        case .basalt: return "Basalt"
        case .chalk:  return "Chalk"
        case .flint:  return "Flint"
        case .onyx:   return "Onyx"
        case .ember:  return "Ember"
        case .birch:  return "Birch"
        case .vapor:  return "Vapor"
        }
    }

    var tagline: LocalizedStringKey {
        switch self {
        case .linen:  return "Ruhig & warm"
        case .slate:  return "Klar & fokussiert"
        case .mist:   return "Sehr sanft"
        case .grove:  return "Natürlich & verbunden"
        case .ink:    return "Editorial & präzise"
        case .copper: return "Warm & abendlich"
        case .abyss:  return "Tief & fokussiert · ND"
        case .dusk:   return "Warm & augenschonend · ND"
        case .basalt: return "Neutral & reizarm · ND"
        case .chalk:  return "Klar & kontrastreich"
        case .flint:  return "Scharf & dunkel"
        case .onyx:   return "Edel & tiefgründig"
        case .ember:  return "Glühend & intensiv"
        case .birch:  return "Natürlich & klar"
        case .vapor:  return "Kühl & präzise"
        }
    }
}

// MARK: - Theme Definition

struct NahbarTheme {
    let id: ThemeID

    // Surfaces
    let backgroundPrimary: Color
    let backgroundSecondary: Color
    let surfaceCard: Color
    let borderSubtle: Color

    // Content
    let contentPrimary: Color
    let contentSecondary: Color
    let contentTertiary: Color
    let accent: Color

    // Typography
    let displayDesign: Font.Design

    // Section Headers
    let sectionHeaderSize: CGFloat
    let sectionHeaderColor: Color

    // Shape
    let radiusCard: CGFloat
    let radiusTag: CGFloat

    // Motion
    let reducedMotion: Bool
}

// MARK: - Theme Definitions

extension NahbarTheme {

    static let linen = NahbarTheme(
        id: .linen,
        backgroundPrimary:   Color(red: 0.961, green: 0.949, blue: 0.933),
        backgroundSecondary: Color(red: 0.929, green: 0.914, blue: 0.894),
        surfaceCard:         Color(red: 0.980, green: 0.969, blue: 0.957),
        borderSubtle:        Color(red: 0.800, green: 0.773, blue: 0.745).opacity(0.45),
        contentPrimary:      Color(red: 0.180, green: 0.145, blue: 0.110),
        contentSecondary:    Color(red: 0.447, green: 0.384, blue: 0.318),
        contentTertiary:     Color(red: 0.620, green: 0.561, blue: 0.494),
        accent:              Color(red: 0.710, green: 0.443, blue: 0.290),
        displayDesign:       .default,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.447, green: 0.384, blue: 0.318),
        radiusCard:          16,
        radiusTag:           8,
        reducedMotion:       false
    )

    static let slate = NahbarTheme(
        id: .slate,
        backgroundPrimary:   Color(red: 0.925, green: 0.933, blue: 0.941),
        backgroundSecondary: Color(red: 0.894, green: 0.906, blue: 0.918),
        surfaceCard:         Color(red: 0.957, green: 0.961, blue: 0.969),
        borderSubtle:        Color(red: 0.729, green: 0.753, blue: 0.784).opacity(0.45),
        contentPrimary:      Color(red: 0.090, green: 0.110, blue: 0.137),
        contentSecondary:    Color(red: 0.353, green: 0.388, blue: 0.431),
        contentTertiary:     Color(red: 0.557, green: 0.596, blue: 0.643),
        accent:              Color(red: 0.239, green: 0.353, blue: 0.945),
        displayDesign:       .default,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.353, green: 0.388, blue: 0.431),
        radiusCard:          12,
        radiusTag:           6,
        reducedMotion:       false
    )

    static let mist = NahbarTheme(
        id: .mist,
        backgroundPrimary:   Color(red: 0.973, green: 0.973, blue: 0.973),
        backgroundSecondary: Color(red: 0.945, green: 0.945, blue: 0.949),
        surfaceCard:         Color(red: 0.988, green: 0.988, blue: 0.992),
        borderSubtle:        Color(red: 0.820, green: 0.820, blue: 0.835).opacity(0.35),
        contentPrimary:      Color(red: 0.200, green: 0.200, blue: 0.216),
        contentSecondary:    Color(red: 0.455, green: 0.455, blue: 0.475),
        contentTertiary:     Color(red: 0.651, green: 0.651, blue: 0.671),
        accent:              Color(red: 0.569, green: 0.541, blue: 0.745),
        displayDesign:       .default,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.455, green: 0.455, blue: 0.475),
        radiusCard:          20,
        radiusTag:           10,
        reducedMotion:       true
    )

    static let grove = NahbarTheme(
        id: .grove,
        backgroundPrimary:   Color(red: 0.910, green: 0.929, blue: 0.894),
        backgroundSecondary: Color(red: 0.875, green: 0.898, blue: 0.855),
        surfaceCard:         Color(red: 0.945, green: 0.957, blue: 0.933),
        borderSubtle:        Color(red: 0.659, green: 0.722, blue: 0.627).opacity(0.45),
        contentPrimary:      Color(red: 0.110, green: 0.188, blue: 0.118),
        contentSecondary:    Color(red: 0.298, green: 0.408, blue: 0.298),
        contentTertiary:     Color(red: 0.467, green: 0.573, blue: 0.455),
        accent:              Color(red: 0.220, green: 0.412, blue: 0.227),
        displayDesign:       .serif,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.298, green: 0.408, blue: 0.298),
        radiusCard:          16,
        radiusTag:           8,
        reducedMotion:       false
    )

    static let ink = NahbarTheme(
        id: .ink,
        backgroundPrimary:   Color(red: 0.980, green: 0.980, blue: 0.976),
        backgroundSecondary: Color(red: 0.953, green: 0.953, blue: 0.949),
        surfaceCard:         Color(red: 1.000, green: 1.000, blue: 1.000),
        borderSubtle:        Color(red: 0.749, green: 0.749, blue: 0.745).opacity(0.45),
        contentPrimary:      Color(red: 0.063, green: 0.063, blue: 0.063),
        contentSecondary:    Color(red: 0.310, green: 0.310, blue: 0.310),
        contentTertiary:     Color(red: 0.541, green: 0.541, blue: 0.541),
        accent:              Color(red: 0.749, green: 0.220, blue: 0.165),
        displayDesign:       .serif,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.310, green: 0.310, blue: 0.310),
        radiusCard:          8,
        radiusTag:           4,
        reducedMotion:       true
    )

    static let copper = NahbarTheme(
        id: .copper,
        backgroundPrimary:   Color(red: 0.110, green: 0.078, blue: 0.063),
        backgroundSecondary: Color(red: 0.141, green: 0.102, blue: 0.082),
        surfaceCard:         Color(red: 0.173, green: 0.129, blue: 0.102),
        borderSubtle:        Color(red: 0.341, green: 0.251, blue: 0.188).opacity(0.55),
        contentPrimary:      Color(red: 0.941, green: 0.902, blue: 0.851),
        contentSecondary:    Color(red: 0.714, green: 0.659, blue: 0.588),
        contentTertiary:     Color(red: 0.502, green: 0.443, blue: 0.373),
        accent:              Color(red: 0.784, green: 0.514, blue: 0.227),
        displayDesign:       .default,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.714, green: 0.659, blue: 0.588),
        radiusCard:          16,
        radiusTag:           8,
        reducedMotion:       false
    )

    // MARK: - Abyss (Neurodivers: kühles Dunkel, Fokus)
    static let abyss = NahbarTheme(
        id: .abyss,
        backgroundPrimary:   Color(red: 0.071, green: 0.082, blue: 0.106),
        backgroundSecondary: Color(red: 0.094, green: 0.110, blue: 0.141),
        surfaceCard:         Color(red: 0.118, green: 0.137, blue: 0.176),
        borderSubtle:        Color(red: 0.251, green: 0.314, blue: 0.431).opacity(0.40),
        contentPrimary:      Color(red: 0.882, green: 0.910, blue: 0.945),
        contentSecondary:    Color(red: 0.541, green: 0.612, blue: 0.710),
        contentTertiary:     Color(red: 0.349, green: 0.408, blue: 0.502),
        accent:              Color(red: 0.357, green: 0.553, blue: 0.937),
        displayDesign:       .default,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.541, green: 0.612, blue: 0.710),
        radiusCard:          14,
        radiusTag:           7,
        reducedMotion:       true
    )

    // MARK: - Dusk (Neurodivers: warmes Dunkel, augenschonend)
    static let dusk = NahbarTheme(
        id: .dusk,
        backgroundPrimary:   Color(red: 0.102, green: 0.082, blue: 0.063),
        backgroundSecondary: Color(red: 0.133, green: 0.110, blue: 0.086),
        surfaceCard:         Color(red: 0.169, green: 0.141, blue: 0.110),
        borderSubtle:        Color(red: 0.353, green: 0.275, blue: 0.204).opacity(0.45),
        contentPrimary:      Color(red: 0.941, green: 0.906, blue: 0.851),
        contentSecondary:    Color(red: 0.651, green: 0.565, blue: 0.451),
        contentTertiary:     Color(red: 0.431, green: 0.357, blue: 0.271),
        accent:              Color(red: 0.831, green: 0.573, blue: 0.271),
        displayDesign:       .default,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.651, green: 0.565, blue: 0.451),
        radiusCard:          18,
        radiusTag:           9,
        reducedMotion:       true
    )

    // MARK: - Basalt (Neurodivers: neutral, reizarm, monospaced)
    static let basalt = NahbarTheme(
        id: .basalt,
        backgroundPrimary:   Color(red: 0.082, green: 0.082, blue: 0.082),
        backgroundSecondary: Color(red: 0.110, green: 0.110, blue: 0.110),
        surfaceCard:         Color(red: 0.141, green: 0.141, blue: 0.141),
        borderSubtle:        Color(red: 0.314, green: 0.314, blue: 0.314).opacity(0.35),
        contentPrimary:      Color(red: 0.882, green: 0.882, blue: 0.882),
        contentSecondary:    Color(red: 0.561, green: 0.561, blue: 0.561),
        contentTertiary:     Color(red: 0.365, green: 0.365, blue: 0.365),
        accent:              Color(red: 0.376, green: 0.725, blue: 0.545),
        displayDesign:       .monospaced,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.561, green: 0.561, blue: 0.561),
        radiusCard:          10,
        radiusTag:           5,
        reducedMotion:       true
    )

    // MARK: - Chalk (Hochkontrast Hell, kostenlos)
    static let chalk = NahbarTheme(
        id: .chalk,
        backgroundPrimary:   Color(red: 0.976, green: 0.976, blue: 0.976),
        backgroundSecondary: Color(red: 0.945, green: 0.945, blue: 0.945),
        surfaceCard:         Color(red: 1.000, green: 1.000, blue: 1.000),
        borderSubtle:        Color(red: 0.690, green: 0.690, blue: 0.690).opacity(0.50),
        contentPrimary:      Color(red: 0.059, green: 0.059, blue: 0.059),
        contentSecondary:    Color(red: 0.267, green: 0.267, blue: 0.267),
        contentTertiary:     Color(red: 0.482, green: 0.482, blue: 0.482),
        accent:              Color(red: 0.196, green: 0.392, blue: 0.902),
        displayDesign:       .default,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.267, green: 0.267, blue: 0.267),
        radiusCard:          12,
        radiusTag:           6,
        reducedMotion:       false
    )

    // MARK: - Flint (Hochkontrast Dunkel, kostenlos)
    static let flint = NahbarTheme(
        id: .flint,
        backgroundPrimary:   Color(red: 0.102, green: 0.102, blue: 0.102),
        backgroundSecondary: Color(red: 0.137, green: 0.137, blue: 0.137),
        surfaceCard:         Color(red: 0.173, green: 0.173, blue: 0.173),
        borderSubtle:        Color(red: 0.376, green: 0.376, blue: 0.376).opacity(0.50),
        contentPrimary:      Color(red: 0.941, green: 0.941, blue: 0.941),
        contentSecondary:    Color(red: 0.651, green: 0.651, blue: 0.651),
        contentTertiary:     Color(red: 0.416, green: 0.416, blue: 0.416),
        accent:              Color(red: 0.220, green: 0.820, blue: 0.796),
        displayDesign:       .default,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.651, green: 0.651, blue: 0.651),
        radiusCard:          12,
        radiusTag:           6,
        reducedMotion:       false
    )

    // MARK: - Onyx (Tiefes Schwarz, Gold-Serif, bezahlt)
    static let onyx = NahbarTheme(
        id: .onyx,
        backgroundPrimary:   Color(red: 0.063, green: 0.063, blue: 0.063),
        backgroundSecondary: Color(red: 0.094, green: 0.094, blue: 0.094),
        surfaceCard:         Color(red: 0.125, green: 0.125, blue: 0.125),
        borderSubtle:        Color(red: 0.310, green: 0.255, blue: 0.176).opacity(0.50),
        contentPrimary:      Color(red: 0.965, green: 0.949, blue: 0.922),
        contentSecondary:    Color(red: 0.647, green: 0.612, blue: 0.557),
        contentTertiary:     Color(red: 0.412, green: 0.380, blue: 0.333),
        accent:              Color(red: 0.835, green: 0.682, blue: 0.286),
        displayDesign:       .serif,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.647, green: 0.612, blue: 0.557),
        radiusCard:          14,
        radiusTag:           7,
        reducedMotion:       false
    )

    // MARK: - Ember (Warmes Dunkel, Orangerot, bezahlt)
    static let ember = NahbarTheme(
        id: .ember,
        backgroundPrimary:   Color(red: 0.110, green: 0.086, blue: 0.078),
        backgroundSecondary: Color(red: 0.145, green: 0.114, blue: 0.102),
        surfaceCard:         Color(red: 0.184, green: 0.149, blue: 0.133),
        borderSubtle:        Color(red: 0.392, green: 0.263, blue: 0.212).opacity(0.50),
        contentPrimary:      Color(red: 0.957, green: 0.918, blue: 0.882),
        contentSecondary:    Color(red: 0.671, green: 0.561, blue: 0.494),
        contentTertiary:     Color(red: 0.435, green: 0.349, blue: 0.298),
        accent:              Color(red: 0.910, green: 0.388, blue: 0.192),
        displayDesign:       .default,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.671, green: 0.561, blue: 0.494),
        radiusCard:          16,
        radiusTag:           8,
        reducedMotion:       false
    )

    // MARK: - Birch (Helles Naturcreme, Waldgrün-Serif, bezahlt)
    static let birch = NahbarTheme(
        id: .birch,
        backgroundPrimary:   Color(red: 0.969, green: 0.961, blue: 0.945),
        backgroundSecondary: Color(red: 0.937, green: 0.925, blue: 0.906),
        surfaceCard:         Color(red: 0.988, green: 0.984, blue: 0.969),
        borderSubtle:        Color(red: 0.682, green: 0.659, blue: 0.612).opacity(0.40),
        contentPrimary:      Color(red: 0.067, green: 0.133, blue: 0.071),
        contentSecondary:    Color(red: 0.227, green: 0.349, blue: 0.224),
        contentTertiary:     Color(red: 0.408, green: 0.502, blue: 0.396),
        accent:              Color(red: 0.118, green: 0.392, blue: 0.153),
        displayDesign:       .serif,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.227, green: 0.349, blue: 0.224),
        radiusCard:          16,
        radiusTag:           8,
        reducedMotion:       false
    )

    // MARK: - Vapor (Kühles Weiß, Tintenblau, Violett, bezahlt)
    static let vapor = NahbarTheme(
        id: .vapor,
        backgroundPrimary:   Color(red: 0.961, green: 0.965, blue: 0.976),
        backgroundSecondary: Color(red: 0.925, green: 0.933, blue: 0.953),
        surfaceCard:         Color(red: 0.988, green: 0.988, blue: 1.000),
        borderSubtle:        Color(red: 0.647, green: 0.671, blue: 0.737).opacity(0.45),
        contentPrimary:      Color(red: 0.047, green: 0.063, blue: 0.145),
        contentSecondary:    Color(red: 0.275, green: 0.306, blue: 0.447),
        contentTertiary:     Color(red: 0.478, green: 0.506, blue: 0.616),
        accent:              Color(red: 0.455, green: 0.255, blue: 0.855),
        displayDesign:       .default,
        sectionHeaderSize:   13,
        sectionHeaderColor:  Color(red: 0.275, green: 0.306, blue: 0.447),
        radiusCard:          14,
        radiusTag:           7,
        reducedMotion:       false
    )

    static func theme(for id: ThemeID) -> NahbarTheme {
        switch id {
        case .linen:  return .linen
        case .slate:  return .slate
        case .mist:   return .mist
        case .grove:  return .grove
        case .ink:    return .ink
        case .copper: return .copper
        case .abyss:  return .abyss
        case .dusk:   return .dusk
        case .basalt: return .basalt
        case .chalk:  return .chalk
        case .flint:  return .flint
        case .onyx:   return .onyx
        case .ember:  return .ember
        case .birch:  return .birch
        case .vapor:  return .vapor
        }
    }
}

// MARK: - Environment

private struct ThemeKey: EnvironmentKey {
    static let defaultValue: NahbarTheme = .linen
}

extension EnvironmentValues {
    var nahbarTheme: NahbarTheme {
        get { self[ThemeKey.self] }
        set { self[ThemeKey.self] = newValue }
    }
}
