import type { Metadata } from "next";
import { unstable_noStore as noStore } from "next/cache";
import Link from "next/link";
import { ArrowLeft, ArrowRight, Mail, Sparkles } from "lucide-react";
import { getLocale, getTranslations } from "next-intl/server";

import { FloatingPreferences } from "@/components/layout/floating-preferences";
import { HeroContentMotion, HeroMotionItem, HeroShell, HeroTitle } from "@/components/layout/site-hero";
import { Button } from "@/components/ui/button";
import { getSiteSettings } from "@/lib/app-config";
import { buildLocalizedMetadata } from "@/lib/metadata";
import { getLocalizedPath, resolveLocale } from "@/lib/locale";

type ComingSoonPageProps = {
  params: Promise<{
    locale: string;
  }>;
};

export const dynamic = "force-dynamic";
export const revalidate = 0;

export async function generateMetadata({ params }: ComingSoonPageProps): Promise<Metadata> {
  noStore();
  await params;
  const siteSettings = await getSiteSettings();
  const localeKey = resolveLocale(await getLocale().catch(() => siteSettings.defaultLocale), siteSettings.defaultLocale);
  const t = await getTranslations({ locale: localeKey, namespace: "comingSoon" });

  return await buildLocalizedMetadata({
    locale: localeKey,
    pathname: "/coming-soon",
    title: siteSettings.locales[localeKey].siteName,
    description: t("description"),
    applyTitleTemplate: false,
  });
}

export default async function ComingSoonPage({
  params,
}: ComingSoonPageProps) {
  noStore();
  await params;
  const siteSettings = await getSiteSettings();
  const localeKey = resolveLocale(await getLocale().catch(() => siteSettings.defaultLocale), siteSettings.defaultLocale);
  const t = await getTranslations({ locale: localeKey, namespace: "comingSoon" });
  const isArabic = localeKey === "ar";
  const DirectionIcon = isArabic ? ArrowLeft : ArrowRight;
  const lines = [
    {
      text: t("titleLineOne"),
      tone: "soft" as const,
      align: isArabic ? "center" as const : "start" as const,
    },
    {
      text: t("titleLineTwo"),
      tone: "default" as const,
      align: isArabic ? "center" as const : "end" as const,
    },
    {
      text: t("titleLineThree"),
      tone: "accent" as const,
      align: isArabic ? "center" as const : "start" as const,
    },
  ];

  return (
    <div className="relative min-h-screen overflow-hidden">
      <FloatingPreferences locale={localeKey} defaultLocale={siteSettings.defaultLocale} />

      <HeroShell className="min-h-screen" showBridges>
        <HeroContentMotion className="relative z-10 w-full">
          <div className="mx-auto max-w-[56rem] text-center">
            <HeroMotionItem>
              <p className="inline-flex items-center gap-2 rounded-pill border border-foreground/8 bg-background/55 px-4 py-2 text-sm font-medium tracking-[0.08em] text-[hsl(var(--hero-ink)/0.84)] shadow-[var(--shadow-sm)] backdrop-blur-[18px] dark:border-foreground/10 dark:bg-background/20 dark:text-foreground/84">
                <Sparkles className="h-3.5 w-3.5 text-brand-secondary" />
                {t("kicker")}
              </p>
            </HeroMotionItem>

            <HeroTitle locale={localeKey} lines={lines} className="mx-auto mt-8 tracking-normal" />

            <HeroMotionItem>
              <p className="mx-auto mt-8 max-w-[34rem] text-sm leading-7 text-foreground/68 sm:text-base">
                {t("description")}
              </p>
            </HeroMotionItem>

            <HeroMotionItem className="mt-8 flex flex-col items-center justify-center gap-3 sm:flex-row">
              <Button asChild size="lg" className="w-full sm:w-auto">
                <Link href={getLocalizedPath(localeKey, "/contact", siteSettings.defaultLocale)}>
                  <Mail className="h-4 w-4" />
                  {t("primaryCta")}
                </Link>
              </Button>
              <Button asChild variant="outline" size="lg" className="w-full sm:w-auto">
                <Link href={getLocalizedPath(localeKey, "/", siteSettings.defaultLocale)}>
                  {t("secondaryCta")}
                  <DirectionIcon className="h-4 w-4" />
                </Link>
              </Button>
            </HeroMotionItem>
          </div>
        </HeroContentMotion>
      </HeroShell>
    </div>
  );
}
