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

export const dynamic = 'force-dynamic';

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

/**
 * Combat (MMA/UFC) fight-cards proxy (Wave 6, S25 rule — Express isn't
 * reachable from the browser directly). Forwards to /api/combat/:date.
 * Off-card windows return an empty-but-valid card list so the UI degrades
 * to the honest empty state, never a crash.
 */
export async function GET(req: NextRequest, { params }: { params: Promise<{ date: string }> }) {
  const { date } = await params;
  const d = String(date || '').toLowerCase();
  try {
    const upstream = await fetch(`${BACKEND_URL}/api/combat/${encodeURIComponent(d)}`, {
      method: 'GET',
      headers: { Accept: 'application/json' },
    });
    const data = await upstream.json().catch(() => ({}));
    if (!upstream.ok) return NextResponse.json(data, { status: upstream.status });
    return NextResponse.json(data);
  } catch {
    return NextResponse.json({ date: d, events: [], source: 'espn' });
  }
}
