import { key } from './redisClient.js';

export function matchKey(matchId) {
  return key(`match:${matchId}`);
}

export function playerKey(userId) {
  return key(`player:${userId}`);
}

export async function saveMatchSnapshot(redis, matchId, snapshot, ttlSeconds) {
  const k = matchKey(matchId);
  await redis.set(k, JSON.stringify(snapshot), { EX: ttlSeconds });
}

export async function loadMatchSnapshot(redis, matchId) {
  const raw = await redis.get(matchKey(matchId));
  if (!raw) return null;
  try {
    return JSON.parse(raw);
  } catch {
    return null;
  }
}
