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

interface Props {
  params: Promise<{ id: string }>;
}

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

  if (!leagueId) {
    notFound();
  }

  const result = await LeagueRulebookPageQuery.execute(leagueId);

  if (result.isErr()) {
    const error = result.getError();
    if (error === 'notFound') {
      notFound();
    }
    // For serverError, show the template with empty data
    return <RulebookTemplate viewData={{
      leagueId,
      gameName: 'Unknown',
      scoringPresetName: 'Unknown',
      championshipsCount: 0,
      sessionTypes: 'None',
      dropPolicySummary: 'Unknown',
      hasActiveDropPolicy: false,
      positionPoints: [],
      bonusPoints: [],
      hasBonusPoints: false,
    }} />;
  }

  return <RulebookTemplate viewData={result.unwrap()} />;
}