import SwiftUI

extension Color {
    // Warm palette matching web app
    static let canvas = Color(hex: "F5EFE6")
    static let surface = Color.white
    static let surfaceSecondary = Color(hex: "F4F4F5")
    static let cardBackground = Color.white
    static let cardSecondary = Color(hex: "F4F4F5")

    static let text1 = Color(hex: "18181B")
    static let text2 = Color(hex: "3F3F46")
    static let text3 = Color(hex: "71717A")
    static let text4 = Color(hex: "A1A1AA")

    // Accent — warm amber/brown
    static let accentWarm = Color(hex: "8B6914")
    static let accentWarmBg = Color(hex: "FEF7E6")

    // Emerald accent from web
    static let accentEmerald = Color(hex: "059669")
    static let accentEmeraldBg = Color(hex: "ECFDF5")

    // Semantic
    static let success = Color(hex: "059669")
    static let error = Color(hex: "DC2626")
    static let warning = Color(hex: "D97706")

    // Macro colors
    static let caloriesColor = Color(hex: "8B6914")
    static let proteinColor = Color(hex: "059669")
    static let carbsColor = Color(hex: "3B82F6")
    static let fatColor = Color(hex: "F59E0B")
    static let sugarColor = Color(hex: "EC4899")
    static let fiberColor = Color(hex: "8B5CF6")

    // Meal colors
    static let breakfast = Color(hex: "F59E0B")
    static let lunch = Color(hex: "059669")
    static let dinner = Color(hex: "3B82F6")
    static let snack = Color(hex: "8B5CF6")

    init(hex: String) {
        let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int: UInt64 = 0
        Scanner(string: hex).scanHexInt64(&int)
        let a, r, g, b: UInt64
        switch hex.count {
        case 3:
            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6:
            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8:
            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
            (a, r, g, b) = (255, 0, 0, 0)
        }
        self.init(
            .sRGB,
            red: Double(r) / 255,
            green: Double(g) / 255,
            blue: Double(b) / 255,
            opacity: Double(a) / 255
        )
    }

    static func mealColor(for meal: String) -> Color {
        switch meal.lowercased() {
        case "breakfast": return .breakfast
        case "lunch": return .lunch
        case "dinner": return .dinner
        case "snack": return .snack
        default: return .text3
        }
    }

    static func mealIcon(for meal: String) -> String {
        switch meal.lowercased() {
        case "breakfast": return "sunrise.fill"
        case "lunch": return "sun.max.fill"
        case "dinner": return "moon.fill"
        case "snack": return "leaf.fill"
        default: return "fork.knife"
        }
    }
}
