import SwiftUI

/// Card theme identifier - stores just the name, colors computed on MainActor
enum CardTheme: String, CaseIterable, Identifiable, Hashable, Sendable {
    case coral = "Coral"
    case midnight = "Midnight"
    case ocean = "Ocean"
    case lime = "Lime"
    case violet = "Violet"
    
    var id: String { rawValue }
    var name: String { rawValue }
    
    var localizedName: String {
        String.localized(rawValue)
    }
    
    static func theme(named name: String) -> CardTheme {
        CardTheme(rawValue: name) ?? .coral
    }
    
    static var all: [CardTheme] { allCases }
    
    // MARK: - Theme Brightness
    
    /// Whether this theme requires dark text for proper contrast.
    /// Light backgrounds need dark text; dark backgrounds need light text.
    var requiresDarkText: Bool {
        switch self {
        case .lime: return true
        case .coral, .midnight, .ocean, .violet: return false
        }
    }
    
    // MARK: - RGB Values (nonisolated)
    
    private var primaryRGB: (Double, Double, Double) {
        switch self {
        case .coral: return (0.95, 0.35, 0.33)
        case .midnight: return (0.12, 0.16, 0.22)
        case .ocean: return (0.08, 0.45, 0.56)
        case .lime: return (0.73, 0.82, 0.34)
        case .violet: return (0.42, 0.36, 0.62)
        }
    }
    
    private var secondaryRGB: (Double, Double, Double) {
        switch self {
        case .coral: return (0.93, 0.83, 0.68)
        case .midnight: return (0.29, 0.33, 0.4)
        case .ocean: return (0.2, 0.65, 0.55)
        case .lime: return (0.93, 0.83, 0.68)
        case .violet: return (0.29, 0.33, 0.4)
        }
    }
    
    private var accentRGB: (Double, Double, Double) {
        switch self {
        case .coral: return (0.95, 0.33, 0.28)
        case .midnight: return (0.95, 0.75, 0.25)
        case .ocean: return (0.95, 0.75, 0.25)
        case .lime: return (0.12, 0.12, 0.14)
        case .violet: return (0.95, 0.75, 0.25)
        }
    }
    
    private var textRGB: (Double, Double, Double) {
        requiresDarkText
            ? (0.14, 0.14, 0.17)  // Dark text for light backgrounds
            : (0.98, 0.98, 0.98)  // Light text for dark backgrounds
    }
    
    // MARK: - Colors (MainActor)
    
    @MainActor var primaryColor: Color {
        Color(red: primaryRGB.0, green: primaryRGB.1, blue: primaryRGB.2)
    }
    
    @MainActor var secondaryColor: Color {
        Color(red: secondaryRGB.0, green: secondaryRGB.1, blue: secondaryRGB.2)
    }
    
    @MainActor var accentColor: Color {
        Color(red: accentRGB.0, green: accentRGB.1, blue: accentRGB.2)
    }
    
    /// The appropriate text color for content displayed on this theme's background.
    @MainActor var textColor: Color {
        Color(red: textRGB.0, green: textRGB.1, blue: textRGB.2)
    }
}
