--[[
    Commander 3.0 - Version Detection

    Detects WoW version/flavor and provides feature flags.
    Used by polyfills to determine what needs shimming.
]]

local ADDON_NAME, C3 = ...

local Version = {}
C3.Version = Version

-- Cache build info
local version, build, date, tocversion = GetBuildInfo()

Version._version = version
Version._build = build
Version._date = date
Version._toc = tocversion

---Get raw build info
---@return table
function Version:GetBuildInfo()
    return {
        version = self._version,
        build = self._build,
        date = self._date,
        toc = self._toc,
    }
end

---Get the game flavor
---@return string "retail"|"cata"|"wrath"|"tbc"|"classic"
function Version:GetFlavor()
    local toc = self._toc

    if toc >= 110000 then
        return "retail"
    elseif toc >= 100000 then
        return "retail"
    elseif toc >= 40000 then
        return "cata"
    elseif toc >= 30000 then
        return "wrath"
    elseif toc >= 20000 then
        return "tbc"
    else
        return "classic"
    end
end

---Check if running on Retail
---@return boolean
function Version:IsRetail()
    return self:GetFlavor() == "retail"
end

---Check if running on any Classic variant
---@return boolean
function Version:IsClassic()
    local flavor = self:GetFlavor()
    return flavor == "classic" or flavor == "tbc" or flavor == "wrath" or flavor == "cata"
end

---Check if running on Classic Era (vanilla)
---@return boolean
function Version:IsClassicEra()
    return self:GetFlavor() == "classic"
end

---Check if running on Wrath Classic
---@return boolean
function Version:IsWrath()
    return self:GetFlavor() == "wrath"
end

---Check if running on Cataclysm Classic
---@return boolean
function Version:IsCata()
    return self:GetFlavor() == "cata"
end

---Check if running on TBC Classic
---@return boolean
function Version:IsTBC()
    return self:GetFlavor() == "tbc"
end

---Check if a specific feature is available
---@param feature string Feature name
---@return boolean
function Version:HasFeature(feature)
    local features = {
        -- API Features
        C_Container = self._toc >= 100000,
        C_Spell = self._toc >= 110000,
        C_MountJournal = self:IsRetail(),
        C_PetJournal = self:IsRetail(),
        C_ToyBox = self:IsRetail(),
        C_TransmogCollection = self:IsRetail() or self:IsCata(),
        C_AzeriteEmpoweredItem = false, -- Deprecated

        -- Game Features
        MountJournal = self:IsRetail(),
        Dragonriding = self:IsRetail() and self._toc >= 100000,
        Skyriding = self:IsRetail() and self._toc >= 110000,
        Transmogrify = self:IsRetail() or self:IsCata(),
        DualSpec = not self:IsClassicEra(),
        Achievements = not self:IsClassicEra(),
        PetBattles = self:IsRetail(),
        Garrisons = self:IsRetail(),
        Warband = self:IsRetail() and self._toc >= 110000,

        -- UI Features
        EditModeManager = self:IsRetail() and self._toc >= 100000,
    }

    return features[feature] or false
end

---Check if a specific API exists
---@param apiPath string e.g., "C_Container.GetContainerNumSlots"
---@return boolean
function Version:HasAPI(apiPath)
    local parts = { strsplit(".", apiPath) }
    local current = _G

    for _, part in ipairs(parts) do
        if current[part] == nil then
            return false
        end
        current = current[part]
    end

    return type(current) == "function"
end

-- Populate feature flags for quick access
C3.Features = {
    isRetail = Version:IsRetail(),
    isClassic = Version:IsClassic(),
    flavor = Version:GetFlavor(),
    toc = Version._toc,
}

C3:Debug("Version:", Version._version, "| Flavor:", Version:GetFlavor(), "| TOC:", Version._toc)
