import { NextResponse } from 'next/server';
import { getServiceRoleSupabase } from '@/lib/supabase';

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

interface ParlayedProp {
  player: string;
  stat: string;
  line: number;
  direction: 'over' | 'under';
  sport: 'NBA' | 'MLB' | 'WNBA';
  parlay_count: number;
  scan_count: number;
  grade?: string;
}

export async function GET() {
  const sb = getServiceRoleSupabase();
  if (!sb) return NextResponse.json({ props: [] });

  const today = new Date().toISOString().slice(0, 10);

  const { data } = await sb
    .from('parlay_leg_frequency')
    .select('player_name, stat, line, over_under, sport, parlay_count, scan_count')
    .eq('game_date', today)
    .order('parlay_count', { ascending: false })
    .limit(10);

  if (!data) return NextResponse.json({ props: [] });

  const props: ParlayedProp[] = data.map((row) => ({
    player: row.player_name,
    stat: row.stat,
    line: Number(row.line),
    direction: row.over_under as 'over' | 'under',
    sport: row.sport as 'NBA' | 'MLB' | 'WNBA',
    parlay_count: row.parlay_count,
    scan_count: row.scan_count,
  }));

  return NextResponse.json({ props });
}
