/**
 * GSC Context Cache — shared between chat route and analysis route.
 * In-memory Map with 5-minute TTL per brand.
 */

interface CachedGsc { data: string; ts: number }
const gscCache = new Map<string, CachedGsc>();
export const GSC_CACHE_TTL = 5 * 60 * 1000;

export function getGscCache(brandId: string): string | null {
  const cached = gscCache.get(`gsc:${brandId}`);
  if (cached && Date.now() - cached.ts < GSC_CACHE_TTL) return cached.data;
  return null;
}

export function setGscCache(brandId: string, data: string): void {
  gscCache.set(`gsc:${brandId}`, { data, ts: Date.now() });
}

export function invalidateGscCache(brandId: string): void {
  for (const key of gscCache.keys()) {
    if (key.startsWith(`gsc:${brandId}`)) gscCache.delete(key);
  }
}
