import SwiftUI
#if canImport(UIKit)
import UIKit
#endif

extension Color {
    static func color(from name: String) -> Color {
        switch name.lowercased() {
        case "red": return .red
        case "orange": return .orange
        case "yellow": return .yellow
        case "green": return .green
        case "mint": return .mint
        case "teal": return .teal
        case "cyan": return .cyan
        case "blue": return .blue
        case "indigo": return .indigo
        case "purple": return .purple
        case "pink": return .pink
        case "brown": return .brown
        default: return .indigo
        }
    }

    /// Brand purple from the app logo (a touch brighter than the icon's `#7023BC`
    /// so it stays legible on the watch's black background). Used for the work phase —
    /// its count-up timer and progress dots.
    static let workTint = Color(red: 0.51, green: 0.22, blue: 0.84)

    /// Light teal counterpart for the rest phase — its count-down timer and dots.
    static let restTint = Color(red: 0.44, green: 0.85, blue: 0.84)

    /// Returns a darker shade by reducing HSB brightness (not opacity).
    func darker(by percentage: CGFloat = 0.2) -> Color {
        #if canImport(UIKit)
        var h: CGFloat = 0, s: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
        guard UIColor(self).getHue(&h, saturation: &s, brightness: &b, alpha: &a) else { return self }
        return Color(hue: Double(h), saturation: Double(s),
                     brightness: Double(max(0, b * (1 - percentage))), opacity: Double(a))
        #else
        return self
        #endif
    }
}

// Canonical palettes for routines (single source of truth).
let availableColors = ["red", "orange", "yellow", "green", "mint", "teal", "cyan", "blue", "indigo", "purple", "pink", "brown"]

let availableIcons = ["dumbbell.fill", "figure.strengthtraining.traditional", "figure.run", "figure.hiking", "figure.cooldown", "figure.boxing", "figure.wrestling", "figure.gymnastics", "figure.handball", "figure.core.training", "heart.fill", "bolt.fill"]
