import { getSiteContent } from "@/lib/db";
import { siteContent as fallback } from "@/data/content";
import type { SiteContent } from "@/types/content";

let cached: { data: SiteContent; expiresAt: number } | null = null;
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes

export function getContent(): SiteContent {
  const now = Date.now();
  if (cached && now < cached.expiresAt) {
    return cached.data;
  }

  try {
    const content = getSiteContent();
    if (content) {
      cached = { data: content, expiresAt: now + CACHE_TTL };
      return content;
    }
    return fallback;
  } catch {
    return fallback;
  }
}

/** Invalidate the content cache (call after admin edits). */
export function invalidateContentCache() {
  cached = null;
}
