import { LeagueStandingsPageQuery } from '@/lib/page-queries/LeagueStandingsPageQuery';
import { LeagueStandingsTemplate } from '@/templates/LeagueStandingsTemplate';
import { notFound } from 'next/navigation';

interface Props {
  params: { id: string };
}

export default async function Page({ params }: Props) {
  const leagueId = params.id;

  if (!leagueId) {
    notFound();
  }

  const result = await LeagueStandingsPageQuery.execute(leagueId);

  if (result.isErr()) {
    const error = result.getError();
    if (error === 'notFound') {
      notFound();
    }
    // For serverError, show the template with empty data
    return <LeagueStandingsTemplate
      viewData={{
        standings: [],
        drivers: [],
        memberships: [],
        leagueId,
        currentDriverId: null,
        isAdmin: false,
      }}
      onRemoveMember={() => {}}
      onUpdateRole={() => {}}
    />;
  }

  return <LeagueStandingsTemplate
    viewData={result.unwrap()}
    onRemoveMember={() => {}}
    onUpdateRole={() => {}}
  />;
}