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

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

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

  if (!leagueId) {
    notFound();
  }

  const result = await LeagueSettingsPageQuery.execute(leagueId);

  if (result.isErr()) {
    const error = result.getError();
    if (error === 'notFound') {
      notFound();
    }
    // For serverError, show the template with empty data
    return <LeagueSettingsTemplate viewData={{
      league: {
        id: leagueId,
        name: 'Unknown League',
        ownerId: 'unknown',
        createdAt: '1970-01-01T00:00:00Z',
      },
      config: {
        basics: {
          name: 'Unknown League',
          description: 'League information unavailable',
          visibility: 'private',
          gameId: 'unknown',
        },
        structure: {
          mode: 'solo',
          maxDrivers: 0,
        },
        championships: {
          enableDriverChampionship: true,
          enableTeamChampionship: false,
          enableNationsChampionship: false,
          enableTrophyChampionship: false,
        },
        scoring: {
          patternId: 'unknown',
        },
        dropPolicy: {
          strategy: 'none',
        },
        timings: {},
        stewarding: {
          decisionMode: 'single_steward',
          requireDefense: false,
          defenseTimeLimit: 24,
          voteTimeLimit: 24,
          protestDeadlineHours: 24,
          stewardingClosesHours: 48,
          notifyAccusedOnProtest: true,
          notifyOnVoteRequired: true,
        },
      },
      presets: [],
      owner: null,
      members: [],
    }} />;
  }

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