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

export const dynamic = 'force-dynamic';

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

/**
 * MLB probable-pitchers proxy (Session 46) — forwards
 * GET /api/schedule/:sport/pitchers to Express (statsapi.mlb.com starters).
 */
export async function GET(req: NextRequest, { params }: { params: Promise<{ sport: string }> }) {
  const { sport } = await params;
  const sportLc = String(sport || '').toLowerCase();
  const qs = req.nextUrl.search;
  try {
    const upstream = await fetch(`${BACKEND_URL}/api/schedule/${encodeURIComponent(sportLc)}/pitchers${qs}`, {
      method: 'GET',
      headers: { Accept: 'application/json' },
    });
    const data = await upstream.json().catch(() => ({ games: [] }));
    return NextResponse.json(data, { status: upstream.ok ? 200 : upstream.status });
  } catch {
    return NextResponse.json({ sport: sportLc, games: [] }, { status: 200 });
  }
}
