import { notFound } from 'next/navigation';
import { PageWrapper } from '@/components/shared/state/PageWrapper';
import { LeaguesTemplate } from '@/templates/LeaguesTemplate';
import { LeaguesPageQuery } from '@/lib/page-queries/page-queries/LeaguesPageQuery';
import type { LeaguesViewData } from '@/lib/view-data/LeaguesViewData';

export default async function Page() {
  // Execute the PageQuery
  const result = await LeaguesPageQuery.execute();
  
  // Handle different result types
  if (result.isErr()) {
    const error = result.getError();
    
    switch (error) {
      case 'notFound':
        notFound();
      case 'redirect':
        // In a real app, this would redirect to login
        notFound();
      case 'LEAGUES_FETCH_FAILED':
      case 'UNKNOWN_ERROR':
      default:
        // Return error state that PageWrapper can handle
        return (
          <PageWrapper
            data={undefined}
            error={new Error('Failed to fetch leagues')}
            Template={LeaguesTemplate}
            errorConfig={{ variant: 'full-screen' }}
          />
        );
    }
  }
  
  const viewData = result.unwrap();
  
  return <PageWrapper data={viewData} Template={LeaguesTemplate} />;
}