--[[
    Commander 3.0 - Flight Style

    Handles Skyriding vs Steady Flight detection and switching.
    Uses SecureAction for combat-safe spell casting and CastProgress
    for live-updating progress bar in terminal output.
]]

local ADDON_NAME, C3 = ...

-- Use shared color utilities
local Colorize = function(...) return C3.Colors.Colorize(...) end

-------------------------------------------------------------------------------
-- Constants
-------------------------------------------------------------------------------

-- Spell/buff IDs for flight style system
local SWITCH_SPELL_ID = 436854          -- Switch Flight Style spell (for SecureAction/IsSpellKnown)
local CAST_TO_STEADY = 460002           -- Cast spell ID when switching TO Steady Flight
local CAST_TO_SKYRIDING = 460003        -- Cast spell ID when switching TO Skyriding
local SKYRIDING_BUFF_ID = 404464        -- Flight Style: Skyriding buff
local STEADY_FLIGHT_BUFF_ID = 404468    -- Flight Style: Steady Flight buff

-------------------------------------------------------------------------------
-- Flight Style API
-------------------------------------------------------------------------------

local Flight = {}
C3.Flight = Flight

-- Export spell IDs for SecureAction and CastProgress
Flight.SWITCH_SPELL_ID = SWITCH_SPELL_ID
Flight.CAST_TO_STEADY = CAST_TO_STEADY
Flight.CAST_TO_SKYRIDING = CAST_TO_SKYRIDING

---Check if player has a specific buff by spell ID
---@param spellID number
---@return boolean|nil True/false if known, nil if data restricted (WoW 12.0+)
local function HasBuff(spellID)
    if not C_UnitAuras or not C_UnitAuras.GetPlayerAuraBySpellID then
        return false
    end
    local aura = C_UnitAuras.GetPlayerAuraBySpellID(spellID)

    -- Handle WoW 12.0+ secret values
    if C3.Secrets and C3.Secrets.IsSecret(aura) then
        return nil  -- Data is restricted
    end

    return aura ~= nil
end

---Get current flight style
---@return string "skyriding", "steady", or "unknown"
function Flight.GetCurrentStyle()
    if HasBuff(SKYRIDING_BUFF_ID) then
        return "skyriding"
    elseif HasBuff(STEADY_FLIGHT_BUFF_ID) then
        return "steady"
    end

    -- Fallback: try checking by name
    local skyridingName = C_Spell and C_Spell.GetSpellName and C_Spell.GetSpellName(SKYRIDING_BUFF_ID)
    if skyridingName then
        local aura = AuraUtil and AuraUtil.FindAuraByName and AuraUtil.FindAuraByName(skyridingName, "player", "HELPFUL")
        if aura then
            return "skyriding"
        end
    end

    return "unknown"
end

---Get styled text with color for a flight style
---@param style string
---@return string
function Flight.GetStyledText(style)
    if style == "skyriding" then
        return Colorize("Skyriding", "accent.primary")
    elseif style == "steady" then
        return Colorize("Steady Flight", "accent.success")
    end
    return Colorize("Unknown", "status.dim")
end

---Check if flight style switching is available
---@return boolean
function Flight.IsAvailable()
    return IsSpellKnown and IsSpellKnown(SWITCH_SPELL_ID)
end

---Get the target style (opposite of current)
---@return string
function Flight.GetTargetStyle()
    local current = Flight.GetCurrentStyle()
    if current == "skyriding" then
        return "steady"
    else
        return "skyriding"
    end
end

---Get the target style name for display
---@return string
function Flight.GetTargetStyleName()
    local target = Flight.GetTargetStyle()
    if target == "skyriding" then
        return "Skyriding"
    else
        return "Steady Flight"
    end
end

-------------------------------------------------------------------------------
-- Command Definition
-------------------------------------------------------------------------------

C3.Command:Define({
    name = "flightstyle",
    description = "Switch between Skyriding and Steady Flight",
    category = "action",
    acceptsInput = false,

    execute = function(ctx)
        -- Check availability
        if not Flight.IsAvailable() then
            return ctx:Error("Flight style switching not available (requires Dragonflight flying)")
        end

        -- Show current style and instruction
        local current = Flight.GetCurrentStyle()
        local target = Flight.GetTargetStyle()

        -- If we reach here, SecureAction wasn't armed (user typed too fast)
        return ctx:Text("Type 'flightstyle' and press Enter to switch to " .. Flight.GetStyledText(target))
    end
})

return Flight
