--[[
============================================================================
ETB - WeaponTypes.lua
============================================================================
Defines weapon types and class restrictions.
Organizes weapons into Blizzard-style categories (One-Handed, Two-Handed, etc.)

USAGE:
    local wt = ETB.WeaponTypes
    local name = wt:GetName("SWORD")  -- "Sword"
    local canWear = wt:CanClassWear("WARRIOR", "SWORD")  -- true
    local group = wt:GetGroup("STAFF")  -- "TWO_HANDED"
============================================================================
]]--

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

ETB.WeaponTypes = {}
local WeaponTypes = ETB.WeaponTypes

-- =============================================================================
-- WEAPON TYPE DEFINITIONS
-- =============================================================================

local WEAPON_TYPE_DATA = {
    -- One-Handed Weapons
    SWORD = {
        name = "Sword",
        group = "ONE_HANDED",
        icon = "Interface\\Icons\\INV_Sword_39",
        invTypes = { "INVTYPE_WEAPON", "INVTYPE_WEAPONMAINHAND", "INVTYPE_WEAPONOFFHAND" },
    },
    AXE = {
        name = "Axe",
        group = "ONE_HANDED",
        icon = "Interface\\Icons\\INV_Axe_01",
        invTypes = { "INVTYPE_WEAPON", "INVTYPE_WEAPONMAINHAND", "INVTYPE_WEAPONOFFHAND" },
    },
    MACE = {
        name = "Mace",
        group = "ONE_HANDED",
        icon = "Interface\\Icons\\INV_Mace_01",
        invTypes = { "INVTYPE_WEAPON", "INVTYPE_WEAPONMAINHAND", "INVTYPE_WEAPONOFFHAND" },
    },
    DAGGER = {
        name = "Dagger",
        group = "ONE_HANDED",
        icon = "Interface\\Icons\\INV_Weapon_ShortBlade_01",
        invTypes = { "INVTYPE_WEAPON", "INVTYPE_WEAPONMAINHAND", "INVTYPE_WEAPONOFFHAND" },
    },
    FIST = {
        name = "Fist Weapon",
        group = "ONE_HANDED",
        icon = "Interface\\Icons\\INV_Gauntlets_04",
        invTypes = { "INVTYPE_WEAPON", "INVTYPE_WEAPONMAINHAND", "INVTYPE_WEAPONOFFHAND" },
    },
    WARGLAIVE = {
        name = "Warglaive",
        group = "ONE_HANDED",
        icon = "Interface\\Icons\\INV_Weapon_Glaive_01",
        invTypes = { "INVTYPE_WEAPON", "INVTYPE_WEAPONMAINHAND", "INVTYPE_WEAPONOFFHAND" },
    },
    
    -- Two-Handed Weapons
    ["2HSWORD"] = {
        name = "Two-Handed Sword",
        group = "TWO_HANDED",
        icon = "Interface\\Icons\\INV_Sword_2h_Ashbringer",
        invTypes = { "INVTYPE_2HWEAPON" },
    },
    ["2HAXE"] = {
        name = "Two-Handed Axe",
        group = "TWO_HANDED",
        icon = "Interface\\Icons\\INV_Axe_09",
        invTypes = { "INVTYPE_2HWEAPON" },
    },
    ["2HMACE"] = {
        name = "Two-Handed Mace",
        group = "TWO_HANDED",
        icon = "Interface\\Icons\\INV_Hammer_05",
        invTypes = { "INVTYPE_2HWEAPON" },
    },
    POLEARM = {
        name = "Polearm",
        group = "TWO_HANDED",
        icon = "Interface\\Icons\\INV_Spear_01",
        invTypes = { "INVTYPE_2HWEAPON" },
    },
    STAFF = {
        name = "Staff",
        group = "TWO_HANDED",
        icon = "Interface\\Icons\\INV_Staff_08",
        invTypes = { "INVTYPE_2HWEAPON" },
    },
    
    -- Ranged Weapons
    BOW = {
        name = "Bow",
        group = "RANGED",
        icon = "Interface\\Icons\\INV_Weapon_Bow_12",
        invTypes = { "INVTYPE_RANGED", "INVTYPE_RANGEDRIGHT" },
    },
    CROSSBOW = {
        name = "Crossbow",
        group = "RANGED",
        icon = "Interface\\Icons\\INV_Weapon_Crossbow_01",
        invTypes = { "INVTYPE_RANGED", "INVTYPE_RANGEDRIGHT" },
    },
    GUN = {
        name = "Gun",
        group = "RANGED",
        icon = "Interface\\Icons\\INV_Weapon_Rifle_01",
        invTypes = { "INVTYPE_RANGED", "INVTYPE_RANGEDRIGHT" },
    },
    WAND = {
        name = "Wand",
        group = "RANGED",
        icon = "Interface\\Icons\\INV_Wand_01",
        invTypes = { "INVTYPE_RANGED", "INVTYPE_RANGEDRIGHT" },
    },
    THROWN = {
        name = "Thrown",
        group = "RANGED",
        icon = "Interface\\Icons\\INV_ThrowingKnife_02",
        invTypes = { "INVTYPE_THROWN" },
    },
    
    -- Off-Hand Items
    SHIELD = {
        name = "Shield",
        group = "OFF_HAND",
        icon = "Interface\\Icons\\INV_Shield_04",
        invTypes = { "INVTYPE_SHIELD" },
    },
    HOLDABLE = {
        name = "Held in Off-Hand",
        group = "OFF_HAND",
        icon = "Interface\\Icons\\INV_Misc_Orb_01",
        invTypes = { "INVTYPE_HOLDABLE" },
    },
}

-- =============================================================================
-- WEAPON GROUPS (Blizzard-style organization)
-- =============================================================================

local WEAPON_GROUPS = {
    ONE_HANDED = {
        name = "One-Handed",
        icon = "Interface\\Icons\\INV_Sword_39",
        order = 1,
    },
    TWO_HANDED = {
        name = "Two-Handed",
        icon = "Interface\\Icons\\INV_Sword_2h_Ashbringer",
        order = 2,
    },
    RANGED = {
        name = "Ranged",
        icon = "Interface\\Icons\\INV_Weapon_Bow_12",
        order = 3,
    },
    OFF_HAND = {
        name = "Off-Hand",
        icon = "Interface\\Icons\\INV_Shield_04",
        order = 4,
    },
}

-- =============================================================================
-- CLASS WEAPON RESTRICTIONS
-- =============================================================================

local CLASS_WEAPON_SKILLS = {
    WARRIOR = {
        SWORD = true, AXE = true, MACE = true, DAGGER = true, FIST = true,
        ["2HSWORD"] = true, ["2HAXE"] = true, ["2HMACE"] = true, POLEARM = true, STAFF = true,
        BOW = true, CROSSBOW = true, GUN = true, THROWN = true,
        SHIELD = true,
    },
    PALADIN = {
        SWORD = true, AXE = true, MACE = true,
        ["2HSWORD"] = true, ["2HAXE"] = true, ["2HMACE"] = true, POLEARM = true,
        SHIELD = true,
    },
    HUNTER = {
        SWORD = true, AXE = true, DAGGER = true, FIST = true, POLEARM = true, STAFF = true,
        ["2HSWORD"] = true, ["2HAXE"] = true,
        BOW = true, CROSSBOW = true, GUN = true,
    },
    ROGUE = {
        SWORD = true, MACE = true, DAGGER = true, FIST = true,
        BOW = true, CROSSBOW = true, GUN = true, THROWN = true,
    },
    PRIEST = {
        MACE = true, DAGGER = true, STAFF = true, WAND = true,
        HOLDABLE = true,
    },
    SHAMAN = {
        AXE = true, MACE = true, DAGGER = true, FIST = true,
        ["2HAXE"] = true, ["2HMACE"] = true, STAFF = true,
        SHIELD = true, HOLDABLE = true,
    },
    MAGE = {
        SWORD = true, DAGGER = true, STAFF = true, WAND = true,
        HOLDABLE = true,
    },
    WARLOCK = {
        SWORD = true, DAGGER = true, STAFF = true, WAND = true,
        HOLDABLE = true,
    },
    DRUID = {
        MACE = true, DAGGER = true, FIST = true, POLEARM = true, STAFF = true,
        ["2HMACE"] = true,
        HOLDABLE = true,
    },
    DEATHKNIGHT = {
        SWORD = true, AXE = true, MACE = true,
        ["2HSWORD"] = true, ["2HAXE"] = true, ["2HMACE"] = true, POLEARM = true,
    },
    MONK = {
        SWORD = true, AXE = true, MACE = true, FIST = true, POLEARM = true, STAFF = true,
    },
    DEMONHUNTER = {
        SWORD = true, AXE = true, FIST = true, WARGLAIVE = true,
    },
    EVOKER = {
        SWORD = true, AXE = true, MACE = true, DAGGER = true, FIST = true,
        ["2HSWORD"] = true, ["2HAXE"] = true, ["2HMACE"] = true, STAFF = true,
        HOLDABLE = true,
    },
}

-- =============================================================================
-- API FUNCTIONS - WEAPON TYPES
-- =============================================================================

function WeaponTypes:Get(weaponType)
    return WEAPON_TYPE_DATA[weaponType]
end

function WeaponTypes:GetName(weaponType)
    local data = WEAPON_TYPE_DATA[weaponType]
    return data and data.name or "Unknown"
end

function WeaponTypes:GetIcon(weaponType)
    local data = WEAPON_TYPE_DATA[weaponType]
    return data and data.icon or "Interface\\Icons\\INV_Misc_QuestionMark"
end

function WeaponTypes:GetGroup(weaponType)
    local data = WEAPON_TYPE_DATA[weaponType]
    return data and data.group or nil
end

function WeaponTypes:GetAll()
    local all = {}
    for key, data in pairs(WEAPON_TYPE_DATA) do
        all[#all + 1] = { key = key, name = data.name, group = data.group, icon = data.icon }
    end
    table.sort(all, function(a, b) return a.name < b.name end)
    return all
end

function WeaponTypes:GetByGroup(groupKey)
    local weapons = {}
    for key, data in pairs(WEAPON_TYPE_DATA) do
        if data.group == groupKey then
            weapons[#weapons + 1] = { key = key, name = data.name, icon = data.icon }
        end
    end
    table.sort(weapons, function(a, b) return a.name < b.name end)
    return weapons
end

-- =============================================================================
-- API FUNCTIONS - WEAPON GROUPS
-- =============================================================================

function WeaponTypes:GetGroupInfo(groupKey)
    return WEAPON_GROUPS[groupKey]
end

function WeaponTypes:GetGroupName(groupKey)
    local data = WEAPON_GROUPS[groupKey]
    return data and data.name or "Unknown"
end

function WeaponTypes:GetAllGroups()
    local groups = {}
    for key, data in pairs(WEAPON_GROUPS) do
        groups[#groups + 1] = { key = key, name = data.name, icon = data.icon, order = data.order }
    end
    table.sort(groups, function(a, b) return a.order < b.order end)
    return groups
end

-- =============================================================================
-- API FUNCTIONS - CLASS RESTRICTIONS
-- =============================================================================

function WeaponTypes:CanClassWear(classToken, weaponType)
    local skills = CLASS_WEAPON_SKILLS[classToken]
    if not skills then return false end
    return skills[weaponType] == true
end

function WeaponTypes:CanCurrentPlayerWear(weaponType)
    local _, classToken = UnitClass("player")
    return self:CanClassWear(classToken, weaponType)
end

function WeaponTypes:GetClassWeapons(classToken)
    local weapons = {}
    local skills = CLASS_WEAPON_SKILLS[classToken]
    if not skills then return weapons end
    
    for weaponType, canUse in pairs(skills) do
        if canUse then
            weapons[#weapons + 1] = weaponType
        end
    end
    return weapons
end

-- =============================================================================
-- CONVERSION HELPERS
-- =============================================================================

-- Map item subtype names to weapon type keys
local SUBTYPE_TO_WEAPON = {
    ["Swords"] = "SWORD",
    ["One-Handed Swords"] = "SWORD",
    ["Two-Handed Swords"] = "2HSWORD",
    ["Axes"] = "AXE",
    ["One-Handed Axes"] = "AXE",
    ["Two-Handed Axes"] = "2HAXE",
    ["Maces"] = "MACE",
    ["One-Handed Maces"] = "MACE",
    ["Two-Handed Maces"] = "2HMACE",
    ["Daggers"] = "DAGGER",
    ["Fist Weapons"] = "FIST",
    ["Polearms"] = "POLEARM",
    ["Staves"] = "STAFF",
    ["Bows"] = "BOW",
    ["Crossbows"] = "CROSSBOW",
    ["Guns"] = "GUN",
    ["Wands"] = "WAND",
    ["Thrown"] = "THROWN",
    ["Shields"] = "SHIELD",
    ["Miscellaneous"] = "HOLDABLE",  -- Off-hands are often "Miscellaneous"
    ["Warglaives"] = "WARGLAIVE",
}

function WeaponTypes:FromSubtypeName(subtypeName)
    return SUBTYPE_TO_WEAPON[subtypeName]
end

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

function WeaponTypes:Print()
    ETB:Print("|cff00ffff[WeaponTypes]|r Weapon groups:")
    for _, group in ipairs(self:GetAllGroups()) do
        ETB:Print(string.format("  %s:", group.name))
        for _, weapon in ipairs(self:GetByGroup(group.key)) do
            ETB:Print(string.format("    - %s", weapon.name))
        end
    end
    
    local _, classToken = UnitClass("player")
    ETB:Print(string.format("\n  Current player (%s) can use:", classToken))
    for _, weaponType in ipairs(self:GetClassWeapons(classToken)) do
        ETB:Print(string.format("    - %s", self:GetName(weaponType)))
    end
end

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