import type { Metadata } from "next"
import { redirect } from "next/navigation"
import { LandingPage } from "@/components/landing/landing-page"
import { MobileRootRedirect } from "@/components/mobile/mobile-root-redirect"
import { IS_MOBILE_BUILD } from "@/lib/platform"
import { suitePageMetadata } from "@/lib/suite/page-metadata"

export const metadata: Metadata = suitePageMetadata({
  app: "suite",
  title: "UltiSuite — La suite collaborative souveraine et open source",
  absoluteTitle: true,
  description:
    "Mails, fichiers, documents collaboratifs, contacts et IA : l'alternative open source et auto-hébergée à Google Workspace et Microsoft 365.",
})

type HomeSearchParams = Promise<{ mail?: string | string[] }>

export default async function Home({
  searchParams,
}: {
  searchParams: HomeSearchParams
}) {
  // Mobile static export: no server runtime, so never read searchParams here.
  if (IS_MOBILE_BUILD) {
    return <MobileRootRedirect />
  }
  const sp = await searchParams
  const raw = sp.mail
  const mail = Array.isArray(raw) ? raw[0] : raw
  if (mail && mail.length > 0) {
    redirect(`/mail/inbox/message/${encodeURIComponent(mail)}`)
  }
  return <LandingPage />
}
