import SwiftUI

extension Color {
    init(hex: String) {
        let hex = hex.trimmingCharacters(in: .alphanumerics.inverted)
        var int: UInt64 = 0
        Scanner(string: hex).scanHexInt64(&int)
        let r, g, b: Double
        r = Double((int >> 16) & 0xFF) / 255.0
        g = Double((int >> 8) & 0xFF) / 255.0
        b = Double(int & 0xFF) / 255.0
        self.init(red: r, green: g, blue: b)
    }
}

enum FlightTheme {
    // MARK: - Adaptive Backgrounds
    // These auto-adapt to light/dark mode
    static let background = Color(.systemGroupedBackground)
    static let cardBackground = Color(.secondarySystemGroupedBackground)
    static let elevatedBackground = Color(.tertiarySystemGroupedBackground)

    // MARK: - Adaptive Text
    static let textPrimary = Color.primary
    static let textSecondary = Color.secondary
    static let textTertiary = Color(.tertiaryLabel)

    // MARK: - Accent & Brand
    static let accent = Color(hex: "6366F1")
    static let accentLight = Color(hex: "818CF8")

    // MARK: - Status Colors (same in both modes)
    static let onTime = Color(hex: "10B981")
    static let delayed = Color(hex: "F59E0B")
    static let cancelled = Color(hex: "EF4444")
    static let boarding = Color(hex: "3B82F6")

    // MARK: - Decorative
    static let routeArc = Color(.separator)
    static let cardShadow = Color.black.opacity(0.08)

    // MARK: - Fonts (airport signage inspired)

    static func airportCode(_ size: CGFloat = 28) -> Font {
        .system(size: size, weight: .bold, design: .rounded)
    }

    static func time(_ size: CGFloat = 20) -> Font {
        .system(size: size, weight: .semibold, design: .monospaced)
    }

    static func flightNumber(_ size: CGFloat = 14) -> Font {
        .system(size: size, weight: .medium, design: .monospaced)
    }

    static func label(_ size: CGFloat = 12) -> Font {
        .system(size: size, weight: .semibold, design: .default)
    }

    // MARK: - Layout

    static let cardCornerRadius: CGFloat = 16
    static let cardPadding: CGFloat = 16
    static let sectionSpacing: CGFloat = 20
}

// MARK: - Card Style Modifier

struct FlightCardStyle: ViewModifier {
    var padding: CGFloat = FlightTheme.cardPadding

    func body(content: Content) -> some View {
        content
            .padding(padding)
            .background(FlightTheme.cardBackground)
            .clipShape(RoundedRectangle(cornerRadius: FlightTheme.cardCornerRadius))
            .shadow(color: FlightTheme.cardShadow, radius: 8, y: 2)
    }
}

extension View {
    func flightCard(padding: CGFloat = FlightTheme.cardPadding) -> some View {
        modifier(FlightCardStyle(padding: padding))
    }
}
