import { NextRequest, NextResponse } from 'next/server';
import { cachedBackendJson, todayKey } from '@/services/odds-cache';

export const dynamic = 'force-dynamic';
export const revalidate = 120;

const VALID_SPORTS = new Set(['NBA', 'MLB', 'WNBA']);

export async function GET(req: NextRequest) {
  const sport = (req.nextUrl.searchParams.get('sport') || 'NBA').toUpperCase();
  if (!VALID_SPORTS.has(sport)) {
    return NextResponse.json({ error: 'Unknown sport.', props: [] }, { status: 400 });
  }

  try {
    const data = await cachedBackendJson<{ props: unknown[] }>(
      todayKey(sport, 'top_graded'),
      sport,
      'top_graded',
      `/api/props/top-graded?sport=${sport}`,
      120,
    );
    return NextResponse.json({ props: Array.isArray(data?.props) ? data.props : [] });
  } catch {
    return NextResponse.json({ props: [] });
  }
}
