import type { LandingPageContent } from '@/components/landing/content';
import { LandingPageBuilder } from '@/components/landing';
import { defaultLandingPageContent } from '@/components/landing/content';
import { RefreshRouteOnSave } from '@/components/payload/refresh-route-on-save';
import { getLandingPageContent } from '@/lib/payload/get-landing-page-content';

type HomeProps = {
  searchParams: Promise<{
    preview?: string | string[];
  }>;
};

const Home = async ({ searchParams }: HomeProps) => {
  const resolvedSearchParams = await searchParams;
  const previewParam = resolvedSearchParams.preview;
  const isPreview = Array.isArray(previewParam)
    ? previewParam.includes('true')
    : previewParam === 'true';

  let content: LandingPageContent;
  try {
    content = await getLandingPageContent(isPreview);
  } catch {
    content = defaultLandingPageContent;
  }

  return (
    <main className='flex min-h-screen flex-col'>
      {isPreview ? <RefreshRouteOnSave /> : null}
      <LandingPageBuilder blocks={content.layout} />
    </main>
  );
};

export default Home;
