local _, ns = ...

local PREY_STATE_SCHEMA_VERSION = 2

function ns:ClearExpiredVaultSnapshot(record)
    if type(record) ~= "table" or type(record.vault) ~= "table" then
        return
    end

    local resetAt = tonumber(record.vault.resetAt)
    if resetAt and resetAt > 0 and time() >= resetAt then
        record.vault = nil
    end
end

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

    local record = self:GetStoredCharacterRecord(key, false)
    self:ClearExpiredVaultSnapshot(record)

    return type(record) == "table" and type(record.vault) == "table" and record.vault.snapshot or nil
end

function ns:ClearExpiredPreyState(record)
    if type(record) ~= "table" or type(record.prey) ~= "table" then
        return
    end

    if tonumber(record.prey.schemaVersion) ~= PREY_STATE_SCHEMA_VERSION then
        record.prey = nil
        return
    end

    local resetAt = tonumber(record.prey.resetAt)
    if resetAt and resetAt > 0 and time() >= resetAt then
        record.prey = nil
    end
end

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

    local record = self:GetStoredCharacterRecord(key, false)
    self:ClearExpiredPreyState(record)

    return type(record) == "table" and type(record.prey) == "table" and record.prey.state or nil
end

function ns:ClearExpiredPvpWeeklyState(record)
    if type(record) ~= "table" or type(record.pvpWeekly) ~= "table" then
        return
    end

    local resetAt = tonumber(record.pvpWeekly.resetAt)
    if resetAt and resetAt > 0 and time() >= resetAt then
        record.pvpWeekly = nil
    end
end

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

    local record = self:GetStoredCharacterRecord(key, false)
    self:ClearExpiredPvpWeeklyState(record)

    return type(record) == "table" and type(record.pvpWeekly) == "table" and record.pvpWeekly.entries or nil
end

function ns:ClearExpiredProfessionWeeklyState(record)
    if type(record) ~= "table" or type(record.professionWeekly) ~= "table" then
        return
    end

    local resetAt = tonumber(record.professionWeekly.resetAt)
    if resetAt and resetAt > 0 and time() >= resetAt then
        record.professionWeekly = nil
    end
end

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

    local record = self:GetStoredCharacterRecord(key, false)
    self:ClearExpiredProfessionWeeklyState(record)

    return type(record) == "table" and type(record.professionWeekly) == "table" and record.professionWeekly.state or nil
end

function ns:ClearExpiredDarkmoonFaireState(record)
    if type(record) ~= "table" or type(record.darkmoonFaire) ~= "table" then
        return
    end

    local state = record.darkmoonFaire
    local dailyResetAt = tonumber(state.dailyResetAt)
    if dailyResetAt and dailyResetAt > 0 and time() >= dailyResetAt then
        record.darkmoonFaire = nil
        return
    end

    local storedDarkmoonKey = state.darkmoonResetKey
    local currentDarkmoonKey = self.GetDarkmoonFaireResetKey and self:GetDarkmoonFaireResetKey() or nil
    if storedDarkmoonKey and currentDarkmoonKey and storedDarkmoonKey ~= currentDarkmoonKey then
        record.darkmoonFaire = nil
    end
end

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

    local record = self:GetStoredCharacterRecord(key, false)
    self:ClearExpiredDarkmoonFaireState(record)

    return type(record) == "table" and type(record.darkmoonFaire) == "table" and record.darkmoonFaire.state or nil
end

function ns:ClearExpiredWorldWeeklyState(record)
    if type(record) ~= "table" or type(record.worldWeekly) ~= "table" then
        return
    end

    local resetAt = tonumber(record.worldWeekly.resetAt)
    if resetAt and resetAt > 0 and time() >= resetAt then
        record.worldWeekly = nil
    end
end

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

    local record = self:GetStoredCharacterRecord(key, false)
    self:ClearExpiredWorldWeeklyState(record)

    return type(record) == "table" and type(record.worldWeekly) == "table" and record.worldWeekly.state or nil
end

function ns:ClearExpiredDailyPveState(record)
    if type(record) ~= "table" or type(record.dailyPve) ~= "table" then
        return
    end

    local resetAt = tonumber(record.dailyPve.resetAt)
    if resetAt and resetAt > 0 and time() >= resetAt then
        record.dailyPve = nil
    end
end

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

    local record = self:GetStoredCharacterRecord(key, false)
    self:ClearExpiredDailyPveState(record)

    return type(record) == "table" and type(record.dailyPve) == "table" and record.dailyPve.state or nil
end

function ns:ClearExpiredMidnightRareState(record)
    if type(record) ~= "table" or type(record.midnightRares) ~= "table" then
        return
    end

    local schemaVersion = tonumber(ns.MIDNIGHT_RARE_STATE_SCHEMA_VERSION) or 1
    if tonumber(record.midnightRares.schemaVersion) ~= schemaVersion then
        record.midnightRares = nil
        return
    end

    local resetAt = tonumber(record.midnightRares.resetAt)
    if resetAt and resetAt > 0 and time() >= resetAt then
        record.midnightRares = nil
    end
end

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

    local record = self:GetStoredCharacterRecord(key, false)
    self:ClearExpiredMidnightRareState(record)

    return type(record) == "table" and type(record.midnightRares) == "table" and record.midnightRares.state or nil
end

function ns:ClearExpiredDelvesState(record)
    if type(record) ~= "table" or type(record.delves) ~= "table" then
        return
    end

    local resetAt = tonumber(record.delves.resetAt)
    if resetAt and resetAt > 0 and time() >= resetAt then
        record.delves = nil
    end
end

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

    local record = self:GetStoredCharacterRecord(key, false)
    self:ClearExpiredDelvesState(record)

    return type(record) == "table" and type(record.delves) == "table" and record.delves.state or nil
end
