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

export const dynamic = 'force-dynamic';

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

/** Live-tracking proxy (A1 Session 11) — forwards GET /api/live/:sport.
 *  The browser hits the Next origin, never Express directly (S25 rule). */
export async function GET(_req: NextRequest, ctx: { params: Promise<{ sport: string }> }) {
  const { sport } = await ctx.params;
  try {
    const upstream = await fetch(`${BACKEND_URL}/api/live/${encodeURIComponent(sport)}`, {
      method: 'GET',
      headers: { Accept: 'application/json' },
    });
    const data = await upstream.json().catch(() => ({ sport, hasLive: false, games: [] }));
    return NextResponse.json(data, { status: upstream.ok ? 200 : upstream.status });
  } catch {
    return NextResponse.json({ sport, hasLive: false, games: [] }, { status: 200 });
  }
}
