--[[
============================================================================
ETB - SourceTypes.lua
============================================================================
Defines item source types (how items are obtained).
Maps Blizzard's sourceType IDs to display info.

USAGE:
    local st = ETB.SourceTypes
    local name = st:GetName(1)  -- "Boss Drop"
    local color = st:GetColor(2)  -- Quest color
    local icon = st:GetIcon(3)  -- Vendor icon
============================================================================
]]--

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

ETB.SourceTypes = {}
local SourceTypes = ETB.SourceTypes

-- =============================================================================
-- SOURCE TYPE DEFINITIONS
-- =============================================================================
-- These IDs match Blizzard's C_TransmogCollection.GetAppearanceSources() sourceType

local SOURCE_TYPE_DATA = {
    [1] = {
        name = "Boss Drop",
        shortName = "Boss",
        color = "|cffff8000",  -- Orange
        icon = "Interface\\Icons\\INV_Misc_Head_Dragon_01",
        description = "Drops from dungeon or raid bosses",
    },
    [2] = {
        name = "Quest",
        shortName = "Quest",
        color = "|cffffff00",  -- Yellow
        icon = "Interface\\Icons\\INV_Misc_QuestionMark",
        description = "Reward from completing quests",
    },
    [3] = {
        name = "Vendor",
        shortName = "Vendor",
        color = "|cffffffff",  -- White
        icon = "Interface\\Icons\\INV_Misc_Coin_01",
        description = "Purchased from vendors",
    },
    [4] = {
        name = "World Drop",
        shortName = "World",
        color = "|cff1eff00",  -- Green
        icon = "Interface\\Icons\\INV_Misc_Bag_10",
        description = "Random drop from creatures in the world",
    },
    [5] = {
        name = "Achievement",
        shortName = "Achieve",
        color = "|cffffff00",  -- Yellow
        icon = "Interface\\Icons\\Achievement_General",
        description = "Reward from achievements",
    },
    [6] = {
        name = "Profession",
        shortName = "Crafted",
        color = "|cffff7c0a",  -- Orange-ish
        icon = "Interface\\Icons\\Trade_BlackSmithing",
        description = "Crafted by professions",
    },
    [7] = {
        name = "Unavailable",
        shortName = "N/A",
        color = "|cff808080",  -- Gray
        icon = "Interface\\Icons\\INV_Misc_QuestionMark",
        description = "No longer obtainable in game",
    },
    [8] = {
        name = "PvP",
        shortName = "PvP",
        color = "|cffff0000",  -- Red
        icon = "Interface\\Icons\\INV_Misc_Token_HonorHold",
        description = "PvP rewards (Honor, Conquest, etc.)",
    },
    [9] = {
        name = "Class Hall",
        shortName = "Class",
        color = "|cffa335ee",  -- Purple
        icon = "Interface\\Icons\\ClassIcon_Warrior",
        description = "Class Hall or Order Hall rewards",
    },
    [10] = {
        name = "Black Market",
        shortName = "BMAH",
        color = "|cffff8000",  -- Orange
        icon = "Interface\\Icons\\INV_Misc_Coin_17",
        description = "Black Market Auction House",
    },
}

-- Default for unknown source types
local UNKNOWN_SOURCE = {
    name = "Unknown",
    shortName = "???",
    color = "|cff808080",
    icon = "Interface\\Icons\\INV_Misc_QuestionMark",
    description = "Source unknown",
}

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

function SourceTypes:Get(sourceTypeID)
    return SOURCE_TYPE_DATA[sourceTypeID] or UNKNOWN_SOURCE
end

function SourceTypes:GetName(sourceTypeID)
    local data = self:Get(sourceTypeID)
    return data.name
end

function SourceTypes:GetShortName(sourceTypeID)
    local data = self:Get(sourceTypeID)
    return data.shortName
end

function SourceTypes:GetColor(sourceTypeID)
    local data = self:Get(sourceTypeID)
    return data.color
end

function SourceTypes:GetColoredName(sourceTypeID)
    local data = self:Get(sourceTypeID)
    return data.color .. data.name .. "|r"
end

function SourceTypes:GetIcon(sourceTypeID)
    local data = self:Get(sourceTypeID)
    return data.icon
end

function SourceTypes:GetDescription(sourceTypeID)
    local data = self:Get(sourceTypeID)
    return data.description
end

function SourceTypes:GetAll()
    local all = {}
    for id, data in pairs(SOURCE_TYPE_DATA) do
        all[#all + 1] = {
            id = id,
            name = data.name,
            shortName = data.shortName,
            color = data.color,
        }
    end
    table.sort(all, function(a, b) return a.id < b.id end)
    return all
end

function SourceTypes:IsValid(sourceTypeID)
    return SOURCE_TYPE_DATA[sourceTypeID] ~= nil
end

-- =============================================================================
-- FORMATTING HELPERS
-- =============================================================================

function SourceTypes:FormatForTooltip(sourceTypeID, location, boss)
    local data = self:Get(sourceTypeID)
    local lines = {}
    
    -- Source type line
    lines[#lines + 1] = data.color .. data.name .. "|r"
    
    -- Location line (if provided)
    if location and location ~= "" then
        lines[#lines + 1] = "|cffffd100Location:|r " .. location
    end
    
    -- Boss line (if provided)
    if boss and boss ~= "" then
        lines[#lines + 1] = "|cffffd100Drops from:|r " .. boss
    end
    
    return lines
end

function SourceTypes:FormatShort(sourceTypeID, location)
    local data = self:Get(sourceTypeID)
    if location and location ~= "" then
        return data.color .. data.shortName .. "|r - " .. location
    end
    return data.color .. data.name .. "|r"
end

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

function SourceTypes:Print()
    ETB:Print("|cff00ffff[SourceTypes]|r Registered source types:")
    for _, st in ipairs(self:GetAll()) do
        ETB:Print(string.format("  %d = %s%s|r", st.id, st.color, st.name))
    end
end

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