local _, ns = ...

local function ensureHousingRuntimeCache(owner)
    owner.housingRuntime = owner.housingRuntime or {
        houseList = {},
        houseLevelFavorByGUID = {},
        lastHouseLevelFavor = nil,
        updatedAt = 0,
    }

    return owner.housingRuntime
end

function ns:RememberHousingHouseList(houseInfoList)
    local cache = ensureHousingRuntimeCache(self)
    cache.houseList = type(houseInfoList) == "table" and houseInfoList or {}
    cache.updatedAt = time()
end

function ns:GetHousingHouseList()
    local cache = ensureHousingRuntimeCache(self)
    return cache.houseList or {}
end

function ns:RememberHousingLevelFavor(houseLevelFavor)
    if type(houseLevelFavor) ~= "table" then
        return
    end

    local cache = ensureHousingRuntimeCache(self)
    local houseGUID = houseLevelFavor.houseGUID

    cache.lastHouseLevelFavor = houseLevelFavor
    if houseGUID then
        cache.houseLevelFavorByGUID[houseGUID] = houseLevelFavor
    end
    cache.updatedAt = time()
end

function ns:GetHousingLevelFavor(houseGUID)
    local cache = ensureHousingRuntimeCache(self)
    if houseGUID and cache.houseLevelFavorByGUID[houseGUID] then
        return cache.houseLevelFavorByGUID[houseGUID]
    end

    return cache.lastHouseLevelFavor
end
