--[[
============================================================================
ETB - Categories.lua
============================================================================
Single source of truth for transmog category definitions.
Maps Blizzard's transmog category IDs to display info.

USAGE:
    local cats = ETB.Categories
    local info = cats:Get(1)  -- Get Head category
    local allArmor = cats:GetArmorCategories()
    local allWeapons = cats:GetWeaponCategories()
============================================================================
]]--

local ADDON_NAME, NS = ...
local ETB = NS.ETB

ETB.Categories = {}
local Categories = ETB.Categories

-- =============================================================================
-- CATEGORY DEFINITIONS
-- =============================================================================
-- These IDs match Blizzard's C_TransmogCollection category system

local CATEGORY_DATA = {
    -- Armor slots (1-11)
    [1]  = { name = "Head",      slot = "INVTYPE_HEAD",     isArmor = true,  order = 1 },
    [2]  = { name = "Shoulder",  slot = "INVTYPE_SHOULDER", isArmor = true,  order = 2 },
    [3]  = { name = "Back",      slot = "INVTYPE_CLOAK",    isArmor = true,  order = 3 },
    [4]  = { name = "Chest",     slot = "INVTYPE_CHEST",    isArmor = true,  order = 4 },
    [5]  = { name = "Shirt",     slot = "INVTYPE_BODY",     isArmor = true,  order = 5 },
    [6]  = { name = "Tabard",    slot = "INVTYPE_TABARD",   isArmor = true,  order = 6 },
    [7]  = { name = "Wrist",     slot = "INVTYPE_WRIST",    isArmor = true,  order = 7 },
    [8]  = { name = "Hands",     slot = "INVTYPE_HAND",     isArmor = true,  order = 8 },
    [9]  = { name = "Waist",     slot = "INVTYPE_WAIST",    isArmor = true,  order = 9 },
    [10] = { name = "Legs",      slot = "INVTYPE_LEGS",     isArmor = true,  order = 10 },
    [11] = { name = "Feet",      slot = "INVTYPE_FEET",     isArmor = true,  order = 11 },
    
    -- Weapon slots (12-13)
    [12] = { name = "Main Hand", slot = "INVTYPE_WEAPON",   isWeapon = true, order = 12 },
    [13] = { name = "Off Hand",  slot = "INVTYPE_HOLDABLE", isWeapon = true, order = 13 },
}

-- Slot type to category ID mapping
local SLOT_TO_CATEGORY = {
    INVTYPE_HEAD = 1,
    INVTYPE_SHOULDER = 2,
    INVTYPE_CLOAK = 3,
    INVTYPE_CHEST = 4,
    INVTYPE_ROBE = 4,  -- Robes count as chest
    INVTYPE_BODY = 5,
    INVTYPE_TABARD = 6,
    INVTYPE_WRIST = 7,
    INVTYPE_HAND = 8,
    INVTYPE_WAIST = 9,
    INVTYPE_LEGS = 10,
    INVTYPE_FEET = 11,
    
    -- Weapons - all map to Main Hand (12) or Off Hand (13)
    INVTYPE_WEAPON = 12,
    INVTYPE_WEAPONMAINHAND = 12,
    INVTYPE_2HWEAPON = 12,
    INVTYPE_RANGED = 12,
    INVTYPE_RANGEDRIGHT = 12,
    INVTYPE_THROWN = 12,
    
    INVTYPE_SHIELD = 13,
    INVTYPE_HOLDABLE = 13,
    INVTYPE_WEAPONOFFHAND = 13,
}

-- =============================================================================
-- API FUNCTIONS
-- =============================================================================

function Categories:Get(categoryID)
    return CATEGORY_DATA[categoryID]
end

function Categories:GetName(categoryID)
    local data = CATEGORY_DATA[categoryID]
    return data and data.name or "Unknown"
end

function Categories:GetSlot(categoryID)
    local data = CATEGORY_DATA[categoryID]
    return data and data.slot or nil
end

function Categories:IsArmor(categoryID)
    local data = CATEGORY_DATA[categoryID]
    return data and data.isArmor or false
end

function Categories:IsWeapon(categoryID)
    local data = CATEGORY_DATA[categoryID]
    return data and data.isWeapon or false
end

function Categories:GetCategoryForSlot(invType)
    return SLOT_TO_CATEGORY[invType]
end

function Categories:GetArmorCategories()
    local armor = {}
    for id, data in pairs(CATEGORY_DATA) do
        if data.isArmor then
            armor[#armor + 1] = { id = id, name = data.name, slot = data.slot, order = data.order }
        end
    end
    table.sort(armor, function(a, b) return a.order < b.order end)
    return armor
end

function Categories:GetWeaponCategories()
    local weapons = {}
    for id, data in pairs(CATEGORY_DATA) do
        if data.isWeapon then
            weapons[#weapons + 1] = { id = id, name = data.name, slot = data.slot, order = data.order }
        end
    end
    table.sort(weapons, function(a, b) return a.order < b.order end)
    return weapons
end

function Categories:GetAll()
    local all = {}
    for id, data in pairs(CATEGORY_DATA) do
        all[#all + 1] = { id = id, name = data.name, slot = data.slot, order = data.order }
    end
    table.sort(all, function(a, b) return a.order < b.order end)
    return all
end

function Categories:GetCount()
    local count = 0
    for _ in pairs(CATEGORY_DATA) do count = count + 1 end
    return count
end

-- =============================================================================
-- DEBUG
-- =============================================================================

function Categories:Print()
    ETB:Print("|cff00ffff[Categories]|r Registered categories:")
    for _, cat in ipairs(self:GetAll()) do
        local catType = self:IsArmor(cat.id) and "Armor" or "Weapon"
        ETB:Print(string.format("  %d = %s (%s, %s)", cat.id, cat.name, cat.slot, catType))
    end
end

ETB:Debug("Categories", "Categories.lua loaded")
