-- ══════════════════════════════════════════════════════════════
-- COMPATIBILITY LAYER - Classic / TBC / Wrath (Multi-Expansion)
-- Wird VOR GuildDeathLog.lua geladen!
-- Schreibt auf addon._compat, Core uebernimmt in GDL table.
-- ══════════════════════════════════════════════════════════════

local addonName, addon = ...

addon._compat = {}

-- ── Versionserkennung via GetBuildInfo ───────────────────────
local tocVersion = select(4, GetBuildInfo())
addon._compat.tocVersion = tocVersion

-- Expansion-Flags (schließt sich gegenseitig aus)
addon._compat.isClassicEra = (tocVersion >= 11000 and tocVersion < 20000)
addon._compat.isTBC        = (tocVersion >= 20000 and tocVersion < 30000)
addon._compat.isWrath      = (tocVersion >= 30000 and tocVersion < 40000)
addon._compat.isCata       = (tocVersion >= 40000 and tocVersion < 50000)
addon._compat.isRetail     = (tocVersion >= 100000)
addon._compat.isClassic    = not (tocVersion >= 100000)

-- Expansion-String
if addon._compat.isClassicEra then
    addon._compat.expansionName = "Classic Era"
elseif addon._compat.isTBC then
    addon._compat.expansionName = "The Burning Crusade"
elseif addon._compat.isWrath then
    addon._compat.expansionName = "Wrath of the Lich King"
elseif addon._compat.isCata then
    addon._compat.expansionName = "Cataclysm"
else
    addon._compat.expansionName = "Unknown"
end

-- Blizzard HC-Todes-Channel nur in Classic Era (Patch 1.15.x)
addon._compat.hasHCChannel    = addon._compat.isClassicEra
addon._compat.hasHardcoreMode = addon._compat.isClassicEra

-- Defensive: einige Client-Staende (u.a. TBC 2.5.x) bevorzugen
-- C_GuildInfo.GuildRoster() und koennen den globalen GuildRoster() entfernt
-- haben. Mehrere Module rufen noch das rohe GuildRoster() auf -> Alias setzen,
-- damit das nie ein "attempt to call a nil value" wird.
if not _G.GuildRoster and C_GuildInfo and C_GuildInfo.GuildRoster then
    _G.GuildRoster = C_GuildInfo.GuildRoster
end

-- ── API-Wrapper ──────────────────────────────────────────────

addon._compat.GuildRoster = function()
    if C_GuildInfo and C_GuildInfo.GuildRoster then
        C_GuildInfo.GuildRoster()
    elseif GuildRoster then
        GuildRoster()
    end
end

addon._compat.SendAddonMessage = function(prefix, message, channel, target)
    if C_ChatInfo and C_ChatInfo.SendAddonMessage then
        return C_ChatInfo.SendAddonMessage(prefix, message, channel, target)
    elseif SendAddonMessage then
        return SendAddonMessage(prefix, message, channel, target)
    end
end

addon._compat.RegisterAddonMessagePrefix = function(prefix)
    if C_ChatInfo and C_ChatInfo.RegisterAddonMessagePrefix then
        return C_ChatInfo.RegisterAddonMessagePrefix(prefix)
    elseif RegisterAddonMessagePrefix then
        return RegisterAddonMessagePrefix(prefix)
    end
end

addon._compat.GetPlayerPosition = function()
    if C_Map and C_Map.GetBestMapForUnit then
        local mapID = C_Map.GetBestMapForUnit("player")
        if mapID then
            local pos = C_Map.GetPlayerMapPosition(mapID, "player")
            if pos then return pos:GetXY() end
        end
    end
    return nil, nil
end

addon._compat.GetSpellInfo = function(spellID)
    if C_Spell and C_Spell.GetSpellInfo then
        local info = C_Spell.GetSpellInfo(spellID)
        return info and info.name or nil
    elseif GetSpellInfo then
        return GetSpellInfo(spellID)
    end
    return nil
end

addon._compat.JoinChannel = function(name, password)
    if JoinChannelByName then
        return JoinChannelByName(name, password or "")
    end
    return nil
end

addon._compat.GetChannelList = function()
    if GetChannelList then
        return GetChannelList()
    end
    return {}
end

addon._compat.PlaySoundFile = function(file, channel)
    if PlaySoundFile then
        PlaySoundFile(file, channel or "Master")
    end
end
