local _, ns = ...

------------------------------------------------------------
-- Gear Tracks: track name -> ordered item levels per rank
------------------------------------------------------------
ns.GEAR_TRACKS = {
    Adventurer = { 220, 224, 227, 230, 233 },
    Veteran    = { 233, 237, 240, 243, 246 },
    Champion   = { 246, 250, 253, 256, 259 },
    Hero       = { 259, 263, 266, 269, 272, 276 },
    Myth       = { 272, 276, 279, 282, 285, 289 },
}

-- Which crest track is used to upgrade each gear track
ns.TRACK_CREST = {
    Adventurer = "Adventurer",
    Veteran    = "Veteran",
    Champion   = "Champion",
    Hero       = "Hero",
    Myth       = "Myth",
}

-- Track ordering (low to high value)
ns.TRACK_ORDER = { "Adventurer", "Veteran", "Champion", "Hero", "Myth" }
ns.TRACK_RANK = {}
for i, t in ipairs(ns.TRACK_ORDER) do
    ns.TRACK_RANK[t] = i
end

------------------------------------------------------------
-- Crafted item level ranges by crest quality
------------------------------------------------------------
ns.CRAFTED_RANGES = {
    Adventurer = { min = 220, max = 233 },
    Veteran    = { min = 233, max = 246 },
    Champion   = { min = 246, max = 259 },
    Hero       = { min = 259, max = 272 },
    Myth       = { min = 272, max = 285 },
}

------------------------------------------------------------
-- Crafting constants
------------------------------------------------------------
ns.CRAFT_CREST_COST = 80              -- crests to craft a spark item (equivalent to 5/6)
ns.VETERAN_EMBELLISH_RESERVE = 160    -- save this many Veteran crests for embellishment crafts

------------------------------------------------------------
-- Key strategic facts
------------------------------------------------------------
-- Champion 6/6 (259) = Hero 2/6 (263 actually, but equiv ~2/6)
-- Champion track is weaker this season — caps much lower relative to Hero
ns.CHAMPION_HERO_EQUIV_RANK = 2  -- champion max ≈ hero rank 2

-- Hero-to-Myth conversion: upgrade hero 5/6 -> 6/6, auto-promotes myth 1/6 -> 2/6
-- This converts 20 Hero crests into 20 Myth crests effectively
ns.HERO_MYTH_CONVERSION_POSSIBLE = true

-- Vault breakpoint value (crests-per-week equivalent bonus)
ns.VAULT_BREAKPOINTS = {
    [3] = 0.40,  -- 3/6 from vault = 40% more crests/week value
    [4] = 0.60,  -- 4/6 from vault = 60% more crests/week value
}

-- Crest save discount is 50% and works for ALL crest types
ns.CREST_SAVE_DISCOUNT = 0.50

------------------------------------------------------------
-- Dungeon loot reference (M+ key level -> loot ilvl, vault ilvl)
------------------------------------------------------------
ns.DUNGEON_LOOT = {
    { key = "Heroic", loot = 230, vault = nil  },
    { key = "M0",     loot = 246, vault = nil  },
    { key = "M2",     loot = 250, vault = 259  },
    { key = "M3",     loot = 250, vault = 259  },
    { key = "M4",     loot = 253, vault = 263  },
    { key = "M5",     loot = 256, vault = 263  },
    { key = "M6",     loot = 259, vault = 266  },
    { key = "M7",     loot = 259, vault = 269  },
    { key = "M8",     loot = 263, vault = 269  },
    { key = "M9",     loot = 263, vault = 269  },
    { key = "M10",    loot = 266, vault = 272  },
    { key = "M11",    loot = 266, vault = 272  },
    { key = "M12",    loot = 266, vault = 272  },
}

------------------------------------------------------------
-- Raid reference anchors
------------------------------------------------------------
ns.RAID_TRACKS = {
    Normal  = "Champion",
    Heroic  = "Hero",
    Mythic  = "Myth",
}

------------------------------------------------------------
-- Slot priority (higher = more valuable to upgrade)
------------------------------------------------------------
ns.SLOT_PRIORITY = {
    [16] = 5, -- Main Hand (weapon)
    [17] = 5, -- Off Hand (weapon)
    [5]  = 4, -- Chest
    [7]  = 4, -- Legs
    [1]  = 4, -- Helm
    [13] = 4, -- Trinket 1
    [14] = 4, -- Trinket 2
    [3]  = 3, -- Shoulder
    [10] = 3, -- Hands
    [8]  = 3, -- Feet
    [11] = 3, -- Ring 1
    [12] = 3, -- Ring 2
    [2]  = 2, -- Neck
    [6]  = 2, -- Waist
    [15] = 2, -- Back
    [9]  = 2, -- Wrist
}

------------------------------------------------------------
-- Crest value tiers (higher = more precious, spend carefully)
------------------------------------------------------------
ns.CREST_VALUE = {
    Adventurer = 1,
    Veteran    = 2,
    Champion   = 3,
    Hero       = 4,
    Myth       = 5,
}

------------------------------------------------------------
-- Player profiles
------------------------------------------------------------
ns.PROFILES = {
    {
        id = "casual",
        name = "Casual / Delves / Normal",
        maxKeyLevel = 0,
        raidTier = "Normal",
        preciousCrests = { "Champion", "Hero" },
        freeCrests = { "Adventurer", "Veteran" },
    },
    {
        id = "heroic_raider",
        name = "Heroic Raider",
        maxKeyLevel = 7,
        raidTier = "Heroic",
        preciousCrests = { "Hero", "Myth" },
        freeCrests = { "Adventurer", "Veteran", "Champion" },
    },
    {
        id = "mythic_raider",
        name = "Mythic Raider",
        maxKeyLevel = 10,
        raidTier = "Mythic",
        preciousCrests = { "Myth" },
        freeCrests = { "Adventurer", "Veteran", "Champion", "Hero" },
    },
    {
        id = "mplus_mid",
        name = "M+ up to 8",
        maxKeyLevel = 8,
        raidTier = "Normal",
        preciousCrests = { "Hero", "Myth" },
        freeCrests = { "Adventurer", "Veteran", "Champion" },
    },
    {
        id = "mplus_high",
        name = "M+ 10+",
        maxKeyLevel = 12,
        raidTier = "Heroic",
        preciousCrests = { "Myth" },
        freeCrests = { "Adventurer", "Veteran", "Champion", "Hero" },
    },
    {
        id = "alt",
        name = "Alt Mode",
        maxKeyLevel = 5,
        raidTier = "Normal",
        preciousCrests = { "Champion", "Hero", "Myth" },
        freeCrests = { "Adventurer", "Veteran" },
    },
}

------------------------------------------------------------
-- Helpers: look up track from item level
------------------------------------------------------------
function ns:GetTrackFromIlvl(ilvl)
    -- Walk tracks from highest to lowest, return first match
    for i = #ns.TRACK_ORDER, 1, -1 do
        local track = ns.TRACK_ORDER[i]
        local levels = ns.GEAR_TRACKS[track]
        if ilvl >= levels[1] and ilvl <= levels[#levels] then
            return track
        end
    end
    return nil
end

function ns:GetRankInTrack(track, ilvl)
    local levels = ns.GEAR_TRACKS[track]
    if not levels then return nil, nil end
    for i, lvl in ipairs(levels) do
        if ilvl == lvl then
            return i, #levels
        end
    end
    return nil, #levels
end

function ns:GetMaxIlvlForTrack(track)
    local levels = ns.GEAR_TRACKS[track]
    if levels then
        return levels[#levels]
    end
    return 0
end

function ns:GetCurrentProfile()
    local profileId = YippYappHelperDB and YippYappHelperDB.profile or "heroic_raider"
    for _, profile in ipairs(ns.PROFILES) do
        if profile.id == profileId then
            return profile
        end
    end
    return ns.PROFILES[2] -- default heroic raider
end

function ns:IsCrestPrecious(crestTrack)
    local profile = ns:GetCurrentProfile()
    for _, t in ipairs(profile.preciousCrests) do
        if t == crestTrack then return true end
    end
    return false
end

function ns:IsCrestFree(crestTrack)
    local profile = ns:GetCurrentProfile()
    for _, t in ipairs(profile.freeCrests) do
        if t == crestTrack then return true end
    end
    return false
end
