import SwiftUI
import UIKit

// BeebBeeb design tokens  sampled from the Figma prototype (screenshots/).
// Spec: documents/design-system.md. This file is the ONLY place a hex may appear.
//
// Swift gotcha: custom Color statics do NOT resolve through leading-dot in generic
// ShapeStyle params. Write `.foregroundStyle(Color.brandInk)`, not `.foregroundStyle(.brandInk)`.
// (`.tint(...)` takes `Color?`, so leading-dot is fine there.)

extension Color {
    init(hex: UInt32) {
        self.init(
            .sRGB,
            red: Double((hex >> 16) & 0xFF) / 255,
            green: Double((hex >> 8) & 0xFF) / 255,
            blue: Double(hex & 0xFF) / 255,
            opacity: 1
        )
    }

    // MARK: Brand  yellow is a verb, one primary action per screen.
    static let brandSoft = Color(hex: 0xFFE04D)
    static let brand = Color(hex: 0xFCD704)
    static let brandDeep = Color(hex: 0xD9B703)
    /// The only valid foreground on brand yellow. Never pure black, never white.
    static let brandInk = Color(hex: 0x0A0B0E)

    /// Kept for source compatibility with the original scaffold.
    static let brandGold = Color.brand

    // MARK: Surface ramp  dark is the primary theme; these five do all the
    // structural work. Depth is elevation, not shadow.
    static let canvas = Color(light: 0xF2F4F7, dark: 0x0A0B0E)
    static let appBackground = Color(light: 0xF6F7F9, dark: 0x0D1014)
    static let surface = Color(light: 0xFFFFFF, dark: 0x13151A)
    static let surfaceRaised = Color(light: 0xFFFFFF, dark: 0x1A1D26)
    static let surfaceOverlay = Color(light: 0xFFFFFF, dark: 0x1E2128)
    static let line = Color(light: 0xE6E8EC, dark: 0x1F2126)
    static let lineStrong = Color(light: 0xCFD4DC, dark: 0x2E323B)
    static let appForeground = Color(light: 0x0D1014, dark: 0xF5F6F8)
    static let muted = Color(light: 0x5B616B, dark: 0x8B919E)
    static let mutedDim = Color(light: 0x868D99, dark: 0x545A66)

    // MARK: Semantic accents (stock 500 hues, as the prototype uses).
    static let success = Color(hex: 0x22C55E)
    static let danger = Color(hex: 0xEF4444)
    static let info = Color(hex: 0x3B82F6)
    static let accentPurple = Color(hex: 0xA855F7)
    static let warning = Color(hex: 0xF59E0B)

    /// A token that resolves per appearance. Dark is the primary theme; light exists
    /// so the app doesn't look broken for riders who force it.
    fileprivate init(light: UInt32, dark: UInt32) {
        self.init(UIColor { trait in
            trait.userInterfaceStyle == .dark
                ? UIColor(Color(hex: dark))
                : UIColor(Color(hex: light))
        })
    }
}

// MARK: - The tint formula (design-system.md §2.5)
//
// Every chip / badge / tinted tile is this one construction. Never hand-pick a tint
// hex. The *label* on a tinted tile stays `Color.muted`  color identifies the
// metric, not the text. That restraint is what keeps five tiles from becoming confetti.

extension Color {
    /// Tinted background: this color at 14% over the card surface.
    var tintFill: Color { opacity(0.14) }
    /// Tinted border: this color at 24%.
    var tintStroke: Color { opacity(0.24) }
}

// MARK: - Shape scale (design-system.md §3)

enum BeebRadius {
    static let chip: CGFloat = 10
    static let field: CGFloat = 14
    static let card: CGFloat = 20
    static let sheet: CGFloat = 28
}

// MARK: - Type scale (design-system.md §4)
//
// Exactly one display-size number per screen: the balance, the timer, or the fare.
// Money and timers are always latin tabular digits  never Arabic-Indic, never mirrored.

extension Font {
    static let beebDisplay = Font.system(size: 56, weight: .bold).monospacedDigit()
    static let beebDisplaySmall = Font.system(size: 48, weight: .bold).monospacedDigit()
    static let beebTitle = Font.system(size: 24, weight: .bold)
    static let beebSection = Font.system(size: 18, weight: .semibold)
    static let beebBody = Font.system(size: 15)
    static let beebLabel = Font.system(size: 13, weight: .medium)
    static let beebCaption = Font.system(size: 11)
    static let beebNumeric = Font.system(size: 15, weight: .semibold).monospacedDigit()
}

extension View {
    /// Keeps figures LTR and tabular so Arabic never reorders money or timers.
    func numeric() -> some View {
        environment(\.layoutDirection, .leftToRight)
            .monospacedDigit()
    }

    /// The tint formula as a modifier: `.tinted(Color.success)`.
    func tinted(_ tone: Color, radius: CGFloat = BeebRadius.chip) -> some View {
        background(tone.tintFill, in: RoundedRectangle(cornerRadius: radius))
            .overlay(
                RoundedRectangle(cornerRadius: radius)
                    .strokeBorder(tone.tintStroke, lineWidth: 1)
            )
            .foregroundStyle(tone)
    }

    /// Glow: the single primary CTA, the ride-status circle, the active nav item.
    /// Nowhere else  glow on every card is how this design starts looking generic.
    func beebGlow(_ tone: Color = .brand) -> some View {
        shadow(color: tone.opacity(0.45), radius: 12, x: 0, y: 0)
    }
}

/// Localization helper  `"key".localized` and `"fmt".localized(arg)`.
extension String {
    var localized: String { NSLocalizedString(self, comment: "") }
    func localized(_ arguments: CVarArg...) -> String {
        String(format: NSLocalizedString(self, comment: ""), arguments: arguments)
    }
}
