import { PageWrapper } from '@/components/shared/state/PageWrapper';
import { HomeTemplate, type HomeViewData } from '@/templates/HomeTemplate';
import { PageDataFetcher } from '@/lib/page/PageDataFetcher';
import { HomePageQuery } from '@/lib/page-queries/HomePageQuery';
import { notFound, redirect } from 'next/navigation';
import { routes } from '@/lib/routing/RouteConfig';

export default async function Page() {
  if (await HomePageQuery.shouldRedirectToDashboard()) {
    redirect(routes.protected.dashboard);
  }

  const data = await PageDataFetcher.fetchManual(async () => {
    const result = await HomePageQuery.execute();
    return result.isOk() ? result.unwrap() : null;
  });
  
  if (!data) {
    notFound();
  }
  
  const Template = ({ viewData }: { viewData: HomeViewData }) => <HomeTemplate viewData={viewData} />;
  
  return <PageWrapper data={data} Template={Template} />;
}
