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

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

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

  if (!leagueId) {
    notFound();
  }

  const result = await LeagueSettingsPageQuery.execute(leagueId);

  if (result.isErr()) {
    const error = result.getError();
    if (error.type === 'notFound') {
      notFound();
    }
    // For serverError, show the template with empty data
    return <LeagueSettingsTemplate viewData={{
      leagueId,
      league: {
        id: leagueId,
        name: 'Unknown League',
        description: 'League information unavailable',
        visibility: 'private',
        ownerId: 'unknown',
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString(),
      },
      config: {
        maxDrivers: 0,
        scoringPresetId: 'unknown',
        allowLateJoin: false,
        requireApproval: false,
      },
    }} />;
  }

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