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

export const dynamic = 'force-dynamic';

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

/**
 * Pitcher arsenal proxy (Wave 5B). Forwards GET
 * /api/stats/pitcher/:name/arsenal to the Express stats route (Baseball Savant
 * pitch mix / velo / whiff). Thin pass-through; preserves ?sport=. On any
 * upstream failure it returns { found:false } so the PitcherArsenal card
 * self-hides honestly rather than showing an error.
 */
export async function GET(req: NextRequest, ctx: { params: Promise<{ name: string }> }) {
  const { name } = await ctx.params;
  const qs = req.nextUrl.search;
  try {
    const upstream = await fetch(
      `${BACKEND_URL}/api/stats/pitcher/${encodeURIComponent(name)}/arsenal${qs}`,
      { method: 'GET', headers: { Accept: 'application/json' } },
    );
    const data = await upstream.json().catch(() => ({ found: false }));
    return NextResponse.json(data, { status: upstream.ok ? 200 : upstream.status });
  } catch {
    return NextResponse.json({ found: false }, { status: 200 });
  }
}
