import { NextRequest, NextResponse } from 'next/server';

export const dynamic = 'force-dynamic';

const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:3000';

/**
 * Snapshot proxy (Session 45) — forwards GET /api/snapshot/:sport (pre-graded slate).
 *
 * Session 67 — the bearer token is FORWARDED so the backend can resolve the
 * caller's tier and decide whether they receive VYNDR's own price. Without
 * this every request would look anonymous and paid users would lose the model
 * leg. The response varies by entitlement, so it is never shared-cached here.
 */
export async function GET(_req: NextRequest, ctx: { params: Promise<{ sport: string }> }) {
  const { sport } = await ctx.params;
  try {
    const auth = _req.headers.get('authorization');
    const upstream = await fetch(`${BACKEND_URL}/api/snapshot/${encodeURIComponent(sport)}`, {
      method: 'GET',
      headers: { Accept: 'application/json', ...(auth ? { Authorization: auth } : {}) },
    });
    const data = await upstream.json().catch(() => ({ grades: [], deltas: [] }));
    return NextResponse.json(data, {
      status: upstream.ok ? 200 : upstream.status,
      headers: { 'Cache-Control': auth ? 'private, max-age=30' : 'public, max-age=30' },
    });
  } catch {
    return NextResponse.json({ sport, grades: [], deltas: [] }, { status: 200 });
  }
}
