import { getPage, getSeoSettings } from "@/lib/api"
import { BlockRenderer } from "@/components/blocks"
import type { Metadata } from "next"

export const revalidate = 60

export async function generateMetadata(): Promise<Metadata> {
  const [page, seoSettings] = await Promise.all([getPage("startseite"), getSeoSettings()])
  if (!page) return { title: 'Martin Porwoll' }

  const seo = seoSettings as unknown as Record<string, unknown> | null
  const titleSuffix = (seo?.metaDefaults as Record<string, unknown>)?.titleSuffix as string || ""
  const title = page.seo?.metaTitle || page.title
  const description = page.seo?.metaDescription || ""

  return {
    title: titleSuffix ? `${title} ${titleSuffix}` : title,
    description,
  }
}

export default async function HomePage() {
  const page = await getPage("startseite")

  if (!page) {
    return (
      <section
        className="relative h-screen w-full bg-cover bg-center bg-fixed"
        style={{ backgroundColor: '#111' }}
      >
        <div className="absolute inset-0 bg-dark/60" />
        <div className="table absolute inset-0 w-full h-full">
          <div className="table-cell w-full h-full align-middle text-center px-4">
            <h1 className="font-heading font-bold text-[3em] md:text-[5em] tracking-[10px] md:tracking-[15px] text-white mb-[50px]">
              MARTIN PORWOLL
            </h1>
            <p className="text-[1.2em] tracking-[2px] text-gray-light">
              Whistleblower. Unternehmer. Mensch.
            </p>
          </div>
        </div>
      </section>
    )
  }

  return <BlockRenderer blocks={page.layout || []} />
}
