local _, ns = ...

-- Midnight Season 1 Dawncrest item IDs
ns.CRESTS = {
    { id = 3383, name = "Adventurer Dawncrest", color = "ff1eff00", track = "Adventurer" },
    { id = 3341, name = "Veteran Dawncrest",    color = "ff0070dd", track = "Veteran"    },
    { id = 3343, name = "Champion Dawncrest",   color = "ffa335ee", track = "Champion"   },
    { id = 3345, name = "Hero Dawncrest",       color = "ffff8000", track = "Hero"       },
    { id = 3347, name = "Myth Dawncrest",       color = "ffff0000", track = "Myth"       },
}

-- Cost per upgrade
ns.CREST_COST_PER_UPGRADE = 20
-- Weekly earning increment (cumulative cap increases by this each week)
ns.CREST_WEEKLY_INCREMENT = 100

function ns:GetCrestInfo()
    local crests = {}
    for _, crest in ipairs(ns.CRESTS) do
        local info = C_CurrencyInfo.GetCurrencyInfo(crest.id)
        if info then
            table.insert(crests, {
                name = crest.name,
                color = crest.color,
                track = crest.track,
                quantity = info.quantity,
                maxQuantity = info.maxQuantity or 0,
                earnedThisWeek = info.quantityEarnedThisWeek or 0,
                weeklyMax = info.maxWeeklyQuantity or 0,
                totalEarned = info.totalEarned or 0,
                seasonCap = info.useTotalEarnedForMaxQty and info.maxQuantity or 0,
                icon = info.iconFileID,
            })
        end
    end
    return crests
end

function ns:GetCrestWeeklyInfo(track)
    for _, crest in ipairs(ns.CRESTS) do
        if crest.track == track then
            local info = C_CurrencyInfo.GetCurrencyInfo(crest.id)
            if info then
                return {
                    quantity = info.quantity,
                    earnedThisWeek = info.quantityEarnedThisWeek or 0,
                    weeklyMax = info.maxWeeklyQuantity or 0,
                    weeklyRemaining = math.max((info.maxWeeklyQuantity or 0) - (info.quantityEarnedThisWeek or 0), 0),
                }
            end
        end
    end
    return { quantity = 0, earnedThisWeek = 0, weeklyMax = 0, weeklyRemaining = 0 }
end

function ns:GetCrestCountByTrack(track)
    for _, crest in ipairs(ns.CRESTS) do
        if crest.track == track then
            local info = C_CurrencyInfo.GetCurrencyInfo(crest.id)
            if info then
                return info.quantity
            end
        end
    end
    return 0
end

function ns:RefreshCrests()
    if not ns.CrestFrames then return end

    local crests = ns:GetCrestInfo()
    local hideDefault = ns._gearAppMode
    for i, frame in ipairs(ns.CrestFrames) do
        local data = crests[i]
        if data then
            frame.icon:SetTexture(data.icon)
            local discountStr = ""
            if ns.HasDiscountAchievement and ns:HasDiscountAchievement(data.track) then
                discountStr = " |cff00ff00-50%|r"
            end

            -- Season cap + weekly info
            local capStr = ""
            if data.seasonCap and data.seasonCap > 0 then
                capStr = string.format("  |cff888888%d/%d|r", data.totalEarned, data.seasonCap)
            end
            local weekStr = ""
            if data.weeklyMax > 0 then
                local weekRemain = math.max(data.weeklyMax - data.earnedThisWeek, 0)
                if weekRemain > 0 then
                    weekStr = string.format("  |cff666666+%dw|r", weekRemain)
                end
            end

            frame.text:SetText(string.format(
                "|c%s%s|r  %d%s%s%s",
                data.color, data.track, data.quantity, discountStr, capStr, weekStr
            ))
            if not hideDefault then frame:Show() end
        else
            frame:Hide()
        end
    end

    -- Also update app-mode crest frames if they exist
    if ns.AppCrestFrames then
        for i, af in ipairs(ns.AppCrestFrames) do
            local data = crests[i]
            if data and af:IsShown() then
                af.icon:SetTexture(data.icon)
                af.countFs:SetText("|cffffffff" .. data.quantity .. "|r")

                local parts = {}
                if data.seasonCap and data.seasonCap > 0 then
                    local seasonRemain = math.max(data.seasonCap - data.totalEarned, 0)
                    local col = seasonRemain <= 0 and "ff555555" or "ff888888"
                    table.insert(parts, "|c" .. col .. data.totalEarned .. "/" .. data.seasonCap .. "|r")
                end
                if data.weeklyMax > 0 then
                    local weekRemain = math.max(data.weeklyMax - data.earnedThisWeek, 0)
                    if weekRemain > 0 then
                        table.insert(parts, "|cff666666+" .. weekRemain .. "w|r")
                    end
                end
                af.upgFs:SetText(table.concat(parts, " "))
            end
        end
    end
end
