-- HDG_TagData.lua
-- Tag-ID → category bucket lookups for housing catalog filter tags.
--
-- Reads the live group→tag map from C_HousingCatalog.GetAllFilterTagGroups()
-- so we don't have to maintain hardcoded tag IDs (Blizzard adds tags every
-- patch). Categorisation is anchored to tag IDs, which are stable across
-- locales — tag NAMES are localized and unsuitable as keys.
--
-- Public API:
--   HDG_TagData.GetCategory(tagID) -> "Sizes"|"Factions"|"Styles"|"Expansions"|"Other"
--   HDG_TagData.Invalidate()       -- clear the cache (test/diagnostic hook)

HDG_TagData = HDG_TagData or {}

-- Map Blizzard's groupName ("Size", "Faction", "Expansion", "Style") to the
-- internal category keys this addon uses elsewhere ("Sizes", "Factions",
-- "Expansions", "Styles"). Anything else is bucketed as "Other".
local GROUP_NAME_TO_CATEGORY = {
    Size      = "Sizes",
    Faction   = "Factions",
    Expansion = "Expansions",
    Style     = "Styles",
}

-- Cached [tagID] → category map, lazy-built on first GetCategory() call.
local _categoryByID

local function buildCategoryMap()
    if _categoryByID then return _categoryByID end
    if not (C_HousingCatalog and C_HousingCatalog.GetAllFilterTagGroups) then return nil end
    local groups = C_HousingCatalog.GetAllFilterTagGroups()
    if not groups then return nil end
    local map = {}
    for _, group in ipairs(groups) do
        local category = GROUP_NAME_TO_CATEGORY[group.groupName or ""]
        if category then
            -- group.tags is an array of { tagID, tagName, anyAssociatedEntries }
            -- — pairs() keys are sequential indices, NOT the real tagIDs.
            for _, tagInfo in pairs(group.tags or {}) do
                if tagInfo and tagInfo.tagID then
                    map[tagInfo.tagID] = category
                end
            end
        end
    end
    _categoryByID = map
    return map
end

function HDG_TagData.GetCategory(tagID)
    local map = buildCategoryMap()
    return (map and map[tagID]) or "Other"
end

function HDG_TagData.Invalidate()
    _categoryByID = nil
end
