import { NextRequest, NextResponse } from 'next/server';
import { getMotocrossStandings } from '@/lib/api/racerxStandings';

export const dynamic = 'force-dynamic';
export const revalidate = 300; // Cache for 5 minutes

export async function GET(
  request: NextRequest,
  { params }: { params: { class: string } }
) {
  try {
    const mxClass = params.class;

    // Validate class parameter
    if (!['450', '250'].includes(mxClass)) {
      return NextResponse.json(
        { error: 'Invalid class. Must be 450 or 250' },
        { status: 400 }
      );
    }

    // Fetch standings from RacerX
    const standings = await getMotocrossStandings(mxClass as any);

    // Return with cache headers
    return NextResponse.json(standings, {
      headers: {
        'Cache-Control': 'public, s-maxage=300, stale-while-revalidate=600'
      }
    });

  } catch (error) {
    console.error('Error fetching MX standings:', error);
    return NextResponse.json(
      { error: 'Failed to fetch Motocross standings' },
      { status: 500 }
    );
  }
}
