--[[
    Commander 3.0 - Mage Teleport/Portal Commands

    Provides teleport and portal commands for Mages.
    Commands: teleport, tp, tele, portal, port

    Uses SecureAction system for combat-safe spellcasting.
]]

local ADDON_NAME, C3 = ...

-------------------------------------------------------------------------------
-- Destination Intellisense Provider
-------------------------------------------------------------------------------

---Get available destinations for the player's faction
---@param isPortal boolean Whether to check portal spells vs teleport
---@return table destinations Array of {name, spellID} pairs
local function getAvailableDestinations(isPortal)
    local _, class = UnitClass("player")
    if class ~= "MAGE" then
        return {}
    end

    local spellTable = isPortal
        and C3.SecureAction.PORTAL_DESTINATIONS
        or C3.SecureAction.TELEPORT_DESTINATIONS

    if not spellTable then
        return {}
    end

    local faction = UnitFactionGroup("player")
    local destinations = {}

    for dest, spellIDs in pairs(spellTable) do
        local allianceID, hordeID, neutralID = spellIDs[1], spellIDs[2], spellIDs[3]
        local spellID

        if faction == "Alliance" then
            spellID = allianceID or neutralID
        elseif faction == "Horde" then
            spellID = hordeID or neutralID
        else
            spellID = neutralID or allianceID or hordeID
        end

        if spellID and IsSpellKnown(spellID) then
            destinations[#destinations + 1] = {
                name = dest,
                spellID = spellID,
            }
        end
    end

    -- Sort alphabetically
    table.sort(destinations, function(a, b)
        return a.name < b.name
    end)

    return destinations
end

---Intellisense provider for teleport destinations
---@param partial string Partial input
---@param ctx table Context
---@return table suggestions
local function teleportProvider(partial, ctx)
    local destinations = getAvailableDestinations(false)

    -- Build suggestions list
    local suggestions = {}
    for _, dest in ipairs(destinations) do
        suggestions[#suggestions + 1] = {
            text = dest.name,
            description = "Teleport",
        }
    end

    -- Add aliases pointing to valid destinations
    local aliases = C3.SecureAction.DESTINATION_ALIASES
    if aliases then
        local validDests = {}
        for _, dest in ipairs(destinations) do
            validDests[dest.name] = true
        end

        for alias, dest in pairs(aliases) do
            if validDests[dest] then
                local displayDest = dest:sub(1, 1):upper() .. dest:sub(2)
                suggestions[#suggestions + 1] = {
                    text = alias,
                    description = "-> " .. displayDest,
                }
            end
        end
    end

    -- Use fuzzy filter
    return C3.Intellisense.fuzzyFilter(suggestions, partial, function(item)
        return item.text
    end, 15)
end

---Intellisense provider for portal destinations
---@param partial string Partial input
---@param ctx table Context
---@return table suggestions
local function portalProvider(partial, ctx)
    local destinations = getAvailableDestinations(true)

    -- Build suggestions list
    local suggestions = {}
    for _, dest in ipairs(destinations) do
        suggestions[#suggestions + 1] = {
            text = dest.name,
            description = "Portal",
        }
    end

    -- Add aliases pointing to valid destinations
    local aliases = C3.SecureAction.DESTINATION_ALIASES
    if aliases then
        local validDests = {}
        for _, dest in ipairs(destinations) do
            validDests[dest.name] = true
        end

        for alias, dest in pairs(aliases) do
            if validDests[dest] then
                local displayDest = dest:sub(1, 1):upper() .. dest:sub(2)
                suggestions[#suggestions + 1] = {
                    text = alias,
                    description = "-> " .. displayDest,
                }
            end
        end
    end

    -- Use fuzzy filter
    return C3.Intellisense.fuzzyFilter(suggestions, partial, function(item)
        return item.text
    end, 15)
end

-------------------------------------------------------------------------------
-- teleport - Teleport to destination (Mage only)
-------------------------------------------------------------------------------

C3.Command:Define({
    name = "teleport",
    description = "Teleport to destination (Mage only)",
    category = "action",
    acceptsInput = false,
    args = {
        {
            name = "destination",
            required = true,
            description = "Destination city",
            provider = teleportProvider,
        }
    },
    execute = function(ctx)
        -- Check if player is a Mage
        local _, class = UnitClass("player")
        if class ~= "MAGE" then
            return ctx:Error("Only Mages can use teleport spells")
        end

        local dest = ctx:GetArg(1)
        if not dest or dest == "" then
            -- List available destinations
            local destinations = getAvailableDestinations(false)
            if #destinations == 0 then
                return ctx:Error("No teleport spells known")
            end

            local names = {}
            for _, d in ipairs(destinations) do
                names[#names + 1] = d.name
            end
            return ctx:Error("Usage: teleport <destination>\nAvailable: " .. table.concat(names, ", "))
        end

        -- SecureAction will handle the actual teleport via pre-armed Enter key
        return ctx:Text("Type 'teleport " .. dest .. "' and press Enter to teleport")
    end
})

-------------------------------------------------------------------------------
-- portal - Open portal to destination (Mage only)
-------------------------------------------------------------------------------

C3.Command:Define({
    name = "portal",
    description = "Open portal to destination (Mage only)",
    category = "action",
    acceptsInput = false,
    args = {
        {
            name = "destination",
            required = true,
            description = "Destination city",
            provider = portalProvider,
        }
    },
    execute = function(ctx)
        -- Check if player is a Mage
        local _, class = UnitClass("player")
        if class ~= "MAGE" then
            return ctx:Error("Only Mages can open portals")
        end

        local dest = ctx:GetArg(1)
        if not dest or dest == "" then
            -- List available destinations
            local destinations = getAvailableDestinations(true)
            if #destinations == 0 then
                return ctx:Error("No portal spells known")
            end

            local names = {}
            for _, d in ipairs(destinations) do
                names[#names + 1] = d.name
            end
            return ctx:Error("Usage: portal <destination>\nAvailable: " .. table.concat(names, ", "))
        end

        -- SecureAction will handle the actual portal via pre-armed Enter key
        return ctx:Text("Type 'portal " .. dest .. "' and press Enter to open portal")
    end
})

return true
