import { NextResponse } from 'next/server';

import { getPlayersForTournament } from '@/lib/data';

export const dynamic = 'force-dynamic';

type RouteContext = {
  params: Promise<{ key: string }>;
};

export async function GET(_request: Request, context: RouteContext) {
  try {
    const { key } = await context.params;
    const data = await getPlayersForTournament(key);
    if (data === null) {
      return NextResponse.json({ error: 'Tournament not found' }, { status: 404 });
    }
    return NextResponse.json(data);
  } catch (e) {
    console.error(e);
    return NextResponse.json([], { status: 500 });
  }
}
