import Foundation

/// The alien's colourways. Add an entry to `all` and it joins both the rotation and the
/// Settings picker automatically — that's the one file to edit to make it your colours.
struct Palette {
    /// Stable key used in config.json — never shown on screen, so the display names can
    /// change without invalidating anyone's saved setting.
    let id: String
    /// What the settings picker calls it. Deliberately just the colour: whoever
    /// downloads this doesn't need to know which of my brands it came from.
    let name: String

    /// Head gradient, light → dark.
    let skin: (String, String, String)
    /// Saucer hull gradient.
    let hull: (String, String, String)
    /// Antenna blip, beam and eye glints.
    let glow: String
    /// Eyebrow text and bubble border.
    let accent: String
    /// Bubble background and the dark outline round the head.
    let ink: String
    /// Outline drawn round the head so it survives white app windows.
    let outline: String

    static let launchpad = Palette(
        id: "launchpad",
        name: "Violet",
        skin: ("#e879f9", "#c026d3", "#7c26c9"),
        hull: ("#08cbf3", "#8439f6", "#4c1d95"),
        glow: "#08cbf3",
        accent: "#08cbf3",
        ink: "#12081f",
        outline: "#2a0d47")

    static let techQueen = Palette(
        id: "tqa",
        name: "Lime",
        skin: ("#fe66c4", "#d4368f", "#8a1f74"),
        hull: ("#d0fd58", "#5ce1e6", "#2f9fb0"),
        glow: "#d0fd58",
        accent: "#d0fd58",
        ink: "#15131f",
        outline: "#3d0f31")

    static let missionControl = Palette(
        id: "mission-control",
        name: "Magenta",
        skin: ("#7fe9ee", "#38bdc8", "#14707a"),
        hull: ("#d63ba0", "#a3186f", "#5b1450"),
        glow: "#d0fd58",
        accent: "#7fe9ee",
        ink: "#14101d",
        outline: "#0a3b41")

    static let all: [Palette] = [.launchpad, .techQueen, .missionControl]

    /// Lookup for the "always use this one" setting. Returns nil for an unknown key,
    /// which the caller treats as "rotate".
    static func named(_ key: String) -> Palette? {
        all.first { $0.id == key }
    }

    /// Advances the rotation and returns the next colourway.
    /// The index lives in UserDefaults so it keeps moving across restarts rather than
    /// resetting to the first one every morning.
    static func advanceRotation() -> Palette {
        let key = "paletteRotationIndex"
        let next = (UserDefaults.standard.integer(forKey: key) + 1) % all.count
        UserDefaults.standard.set(next, forKey: key)
        return all[next]
    }
}
