/**
 * 缓存管理工具
 */

const REWARD_CACHE_KEY = 'reward_cache_v2'  // v2: 修复概率精度问题
const MATCHING_GAME_CACHE_KEY = 'matching_game_cache_v1'

/**
 * 判断缓存是否新鲜
 * @param {number} timestamp - 缓存时间戳
 * @param {number} ttl - 有效期（毫秒），默认24小时
 * @returns {boolean}
 */
export function isFresh(timestamp, ttl = 24 * 60 * 60 * 1000) {
    const now = Date.now()
    const v = Number(timestamp || 0)
    return now - v < ttl
}

/**
 * 获取奖励缓存
 * @returns {Object}
 */
export function getRewardCache() {
    const obj = uni.getStorageSync(REWARD_CACHE_KEY) || {}
    return typeof obj === 'object' && obj ? obj : {}
}

/**
 * 设置奖励缓存
 * @param {string} activityId - 活动ID
 * @param {string} issueId - 期ID
 * @param {any} value - 缓存值
 */
export function setRewardCache(activityId, issueId, value) {
    const cache = getRewardCache()
    const act = cache[activityId] || {}
    act[issueId] = { value, ts: Date.now() }
    cache[activityId] = act
    uni.setStorageSync(REWARD_CACHE_KEY, cache)
}

/**
 * 获取奖励缓存项
 * @param {string} activityId - 活动ID
 * @param {string} issueId - 期ID
 * @returns {any|null}
 */
export function getRewardCacheItem(activityId, issueId) {
    const cache = getRewardCache()
    const act = cache[activityId] || {}
    const c = act[issueId]
    if (c && isFresh(c.ts) && Array.isArray(c.value)) {
        return c.value
    }
    return null
}

/**
 * 获取对对碰游戏缓存
 * @returns {Object}
 */
export function getMatchingGameCache() {
    const obj = uni.getStorageSync(MATCHING_GAME_CACHE_KEY) || {}
    return typeof obj === 'object' && obj ? obj : {}
}

/**
 * 读取对对碰游戏缓存项
 * @param {string} activityId - 活动ID
 * @param {string} issueId - 期ID
 * @returns {Object|null}
 */
export function readMatchingGameCacheEntry(activityId, issueId) {
    const activityKey = String(activityId || '')
    const issueKey = String(issueId || '')
    if (!activityKey || !issueKey) return null
    const cache = getMatchingGameCache()
    const act = cache[activityKey] || {}
    const entry = act && act[issueKey]
    const ok = entry && typeof entry === 'object' && entry.game_id
    return ok ? entry : null
}

/**
 * 写入对对碰游戏缓存项
 * @param {string} activityId - 活动ID
 * @param {string} issueId - 期ID
 * @param {Object} entry - 缓存数据
 */
export function writeMatchingGameCacheEntry(activityId, issueId, entry) {
    const activityKey = String(activityId || '')
    const issueKey = String(issueId || '')
    if (!activityKey || !issueKey) return
    const cache = getMatchingGameCache()
    const act = (cache[activityKey] && typeof cache[activityKey] === 'object') ? cache[activityKey] : {}
    act[issueKey] = entry
    cache[activityKey] = act
    uni.setStorageSync(MATCHING_GAME_CACHE_KEY, cache)
}

/**
 * 清除对对碰游戏缓存项
 * @param {string} activityId - 活动ID
 * @param {string} issueId - 期ID
 */
export function clearMatchingGameCacheEntry(activityId, issueId) {
    const activityKey = String(activityId || '')
    const issueKey = String(issueId || '')
    const cache = getMatchingGameCache()
    const act = cache[activityKey]
    if (!act || typeof act !== 'object') return
    if (act[issueKey] !== undefined) delete act[issueKey]
    if (Object.keys(act).length === 0) delete cache[activityKey]
    else cache[activityKey] = act
    uni.setStorageSync(MATCHING_GAME_CACHE_KEY, cache)
}

/**
 * 查找最新的对对碰游戏缓存
 * @param {string} activityId - 活动ID
 * @returns {Object|null}
 */
export function findLatestMatchingGameCacheEntry(activityId) {
    const activityKey = String(activityId || '')
    if (!activityKey) return null
    const cache = getMatchingGameCache()
    const act = cache[activityKey]
    if (!act || typeof act !== 'object') return null
    let bestIssueId = ''
    let bestEntry = null
    let bestTs = -Infinity
    Object.keys(act).forEach(issueId => {
        const entry = act[issueId]
        if (!entry || typeof entry !== 'object' || !entry.game_id) return
        const ts = Number(entry.ts || 0)
        if (!bestEntry || ts > bestTs) {
            bestTs = ts
            bestIssueId = issueId
            bestEntry = entry
        }
    })
    if (!bestEntry) return null
    return { issue_id: bestIssueId, entry: bestEntry }
}
