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 = 3348, 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,
                earnedThisWeek = info.quantityEarnedThisWeek or 0,
                weeklyMax = info.maxWeeklyQuantity or 0,
                totalEarned = info.totalEarned 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()
    for i, frame in ipairs(ns.CrestFrames) do
        local data = crests[i]
        if data then
            frame.icon:SetTexture(data.icon)
            local cost = ns:GetCrestCost(data.track)
            local upgrades = cost > 0 and math.floor(data.quantity / cost) or 0
            local discountStr = ""
            if ns.HasDiscountAchievement and ns:HasDiscountAchievement(data.track) then
                discountStr = " |cff00ff00-50%|r"
            end
            local canEarnStr = ""
            if data.weeklyMax > 0 then
                local canEarn = math.max(data.weeklyMax - (data.totalEarned or 0), 0)
                if canEarn > 0 then
                    canEarnStr = string.format("  |cff888888(%d earnable)|r", canEarn)
                end
            end
            frame.text:SetText(string.format(
                "|c%s%s|r  %d%s  |cffcccccc%d upg|r%s",
                data.color, data.track, data.quantity, discountStr, upgrades, canEarnStr
            ))
            frame:Show()
        else
            frame:Hide()
        end
    end
end
