type MatchLike = {
    competition_id?: number | string
    league_name?: string
}

const WORLD_CUP_COMPETITION_ID = 61
const WORLD_CUP_SOURCE_UTC_OFFSET_SECONDS = 8 * 60 * 60

export const isWorldCupMatch = (match?: MatchLike | null): boolean => {
    if (!match) return false
    return Number(match.competition_id || 0) === WORLD_CUP_COMPETITION_ID
        || String(match.league_name || '') === '世界杯'
}

export const resolveMatchTimestamp = (timestamp: number | string | null | undefined, match?: MatchLike | null): number => {
    const seconds = Number(timestamp || 0)
    if (!Number.isFinite(seconds) || seconds <= 0) return 0

    // World Cup source fields are UTC clock values saved as server-local Unix seconds.
    if (isWorldCupMatch(match)) return seconds + WORLD_CUP_SOURCE_UTC_OFFSET_SECONDS

    return seconds
}

export const getLocalMatchDate = (timestamp: number | string | null | undefined, match?: MatchLike | null): Date | null => {
    const seconds = resolveMatchTimestamp(timestamp, match)
    if (!seconds) return null
    return new Date(seconds * 1000)
}
