local addonName, NS = ...

-- ============================================================================
-- VAULTSKINS: UI COMPATIBILITY
-- ============================================================================

local Compatibility = {}
NS.Compatibility = Compatibility

-- ============================ SUPPORTED UI SKINS ============================
local KNOWN_UI_SKINS = {
    { id = "ElvUI", label = "ElvUI" },
    { id = "Tukui", label = "Tukui" },
    { id = "Aurora", label = "Aurora" },
    { id = "NDui", label = "NDui" },
    { id = "KkthnxUI", label = "KkthnxUI" },
}

-- ============================ DETECTION HELPERS =============================
local function IsLoaded(addon)
    if C_AddOns and C_AddOns.IsAddOnLoaded then
        return C_AddOns.IsAddOnLoaded(addon)
    end
    return IsAddOnLoaded and IsAddOnLoaded(addon) or false
end

-- ========================= PUBLIC COMPATIBILITY API =========================
function Compatibility:Refresh()
    self.loadedSkins = {}
    for _, entry in ipairs(KNOWN_UI_SKINS) do
        if IsLoaded(entry.id) then
            self.loadedSkins[#self.loadedSkins + 1] = entry.label
        end
    end
    self.externalSkinLoaded = #self.loadedSkins > 0
end

function Compatibility:GetLoadedSkins()
    if not self.loadedSkins then
        self:Refresh()
    end
    return self.loadedSkins
end

function Compatibility:HasExternalSkin()
    if self.externalSkinLoaded == nil then
        self:Refresh()
    end
    return self.externalSkinLoaded
end

function Compatibility:GetMode()
    local mode = VaultSkinsDB and VaultSkinsDB.compatibilityMode or "auto"
    if mode ~= "auto" and mode ~= "native" and mode ~= "minimal" then
        mode = "auto"
    end
    return mode
end

function Compatibility:UseMinimalButtonSkin()
    local mode = self:GetMode()
    if mode == "minimal" then
        return true
    end
    if mode == "native" then
        return false
    end
    return self:HasExternalSkin()
end

function Compatibility:GetStatusText()
    local loaded = self:GetLoadedSkins()
    if #loaded == 0 then
        return "Blizzard UI detected"
    end
    return table.concat(loaded, ", ") .. " detected"
end

Compatibility:Refresh()
