local _, ns = ...

local Util = ns.Util or {}
local getWeeklyResetInfo = Util.GetWeeklyResetInfo

local WorldWeeklyEvents = ns.WorldWeeklyEvents or {}
local WorldWeeklyWorldRows = ns.WorldWeeklyWorldRows or {}

local function buildDisplaySummary(rows)
    local completed = 0
    local total = 0

    for _, row in ipairs(rows or {}) do
        if row and row.excludeFromSummary ~= true then
            total = total + 1
            if row.status == "complete" or row.completed == true then
                completed = completed + 1
            end
        end
    end

    return {
        completed = completed,
        open = math.max(0, total - completed),
        total = total,
        text = string.format("%d/%d", completed, total),
    }
end

function ns:BuildCurrentWorldWeeklyState(fallbackOverview)
    local worldRows = WorldWeeklyWorldRows.BuildRows and WorldWeeklyWorldRows.BuildRows(fallbackOverview) or {}
    local eventRows = WorldWeeklyEvents.BuildRows and WorldWeeklyEvents.BuildRows(fallbackOverview) or {}

    local keyCurrency, dundunCurrency
    if WorldWeeklyWorldRows.BuildCurrencies then
        keyCurrency, dundunCurrency = WorldWeeklyWorldRows.BuildCurrencies()
    end

    local _, weeklyResetAt = getWeeklyResetInfo()
    local currentKey = self:GetCurrentCharacterInfo().key
    local weeklyRows = {
        spark = self:GetLiveSparkWeeklyState(currentKey, nil),
        rotating_world_weekly = self:GetLiveRotatingDungeonWeeklyState(currentKey, nil),
        rotating_world_aethas = self:GetLiveAethasWeeklyState(currentKey, nil),
        housing = self:GetLiveHousingWeeklyState(currentKey, nil),
    }

    return {
        generatedAt = time(),
        resetAt = weeklyResetAt,
        worldRows = worldRows,
        worldSummary = WorldWeeklyWorldRows.BuildSummary and WorldWeeklyWorldRows.BuildSummary(worldRows, keyCurrency, dundunCurrency) or buildDisplaySummary(worldRows),
        keyCurrency = keyCurrency,
        dundunCurrency = dundunCurrency,
        weeklyRows = weeklyRows,
        events = eventRows,
        eventSummary = WorldWeeklyEvents.BuildSummary and WorldWeeklyEvents.BuildSummary(eventRows) or buildDisplaySummary(eventRows),
    }
end

function ns:RefreshCurrentCharacterWorldWeeklyData()
    local record = self:RefreshCurrentCharacterRecord()
    self:ClearExpiredWorldWeeklyState(record)

    local fallbackOverview
    local liveCharacter = self:GetLiveVaultloomCharacterRecord(record.key)
    if type(liveCharacter) == "table" then
        local currentWeek = liveCharacter.currentWeek
        if type(currentWeek) == "string" and currentWeek ~= "" then
            local weeklyRecord = type(liveCharacter.weekly) == "table" and liveCharacter.weekly[currentWeek] or nil
            fallbackOverview = type(weeklyRecord) == "table"
                and type(weeklyRecord.snapshot) == "table"
                and weeklyRecord.snapshot.weeklyOverview
                or nil
        end
    end

    local state = self:BuildCurrentWorldWeeklyState(fallbackOverview)
    if not state then
        return nil
    end

    record.worldWeekly = {
        resetAt = tonumber(state.resetAt) or select(2, getWeeklyResetInfo()),
        state = state,
    }

    return state
end

function ns:GetLiveWorldWeeklyState(keyOrCharacter, fallbackState)
    local key = keyOrCharacter
    if type(keyOrCharacter) == "table" then
        key = keyOrCharacter.key
    end

    if self:IsCurrentCharacterKey(key) then
        local liveState = self:BuildCurrentWorldWeeklyState(fallbackState)
        if liveState then
            return liveState
        end
    end

    local storedState = self:GetStoredWorldWeeklyState(key)
    if storedState then
        return storedState
    end

    return fallbackState
end
