local _, ns = ...

local function safeCopyTable(source)
    local copy = {}
    if type(source) ~= "table" then
        return copy
    end

    for key, value in pairs(source) do
        copy[key] = value
    end

    return copy
end

function ns:RememberMythicPlusSnapshot(snapshot, keyOrCharacter)
    if type(snapshot) ~= "table" then
        return nil
    end

    local key = keyOrCharacter
    if type(keyOrCharacter) == "table" then
        key = keyOrCharacter.key
    end

    local record = self:GetStoredCharacterRecord(key, true)
    if type(record) ~= "table" then
        return nil
    end

    local snapshotCopy = safeCopyTable(snapshot)
    local updatedAt = tonumber(snapshot.updatedAt) or time()
    snapshotCopy.updatedAt = updatedAt
    snapshotCopy.isSnapshot = nil
    record.mythicPlus = {
        updatedAt = updatedAt,
        snapshot = snapshotCopy,
    }

    return snapshotCopy
end

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

    local record = self:GetStoredCharacterRecord(key, false)
    local bucket = type(record) == "table" and type(record.mythicPlus) == "table" and record.mythicPlus or nil
    local snapshot = type(bucket) == "table" and type(bucket.snapshot) == "table" and bucket.snapshot or nil
    if not snapshot then
        return nil
    end

    local snapshotCopy = safeCopyTable(snapshot)
    snapshotCopy.updatedAt = tonumber(bucket.updatedAt) or tonumber(snapshot.updatedAt) or nil
    snapshotCopy.isSnapshot = true
    return snapshotCopy
end

function ns:RememberHousingSnapshot(snapshot, keyOrCharacter)
    if type(snapshot) ~= "table" then
        return nil
    end

    local key = keyOrCharacter
    if type(keyOrCharacter) == "table" then
        key = keyOrCharacter.key
    end

    local record = self:GetStoredCharacterRecord(key, true)
    if type(record) ~= "table" then
        return nil
    end

    local snapshotCopy = safeCopyTable(snapshot)
    local updatedAt = tonumber(snapshot.updatedAt) or time()
    snapshotCopy.updatedAt = updatedAt
    snapshotCopy.isSnapshot = nil
    record.housing = {
        updatedAt = updatedAt,
        snapshot = snapshotCopy,
    }

    return snapshotCopy
end

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

    local record = self:GetStoredCharacterRecord(key, false)
    local bucket = type(record) == "table" and type(record.housing) == "table" and record.housing or nil
    local snapshot = type(bucket) == "table" and type(bucket.snapshot) == "table" and bucket.snapshot or nil
    if not snapshot then
        return nil
    end

    local snapshotCopy = safeCopyTable(snapshot)
    snapshotCopy.updatedAt = tonumber(bucket.updatedAt) or tonumber(snapshot.updatedAt) or nil
    snapshotCopy.isSnapshot = true
    return snapshotCopy
end
