import Link from "next/link";
import Image from "next/image";
import { notFound } from "next/navigation";
import { ArrowRight } from "lucide-react";
import { isLocale, type Locale } from "@/lib/i18n/config";
import { getDictionary } from "@/lib/i18n";
import { buildMetadata } from "@/lib/seo";
import { Button } from "@/components/ui/button";

export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }) {
  const { locale: raw } = await params;
  if (!isLocale(raw)) return {};
  const locale = raw as Locale;
  const dict = getDictionary(locale);
  return buildMetadata({
    locale,
    title: `${dict.story.title} — DAR ZELLIGE`,
    description: dict.story.subtitle,
    path: "/story",
    image: "/images/hero-main.jpg",
  });
}

export default async function StoryPage({ params }: { params: Promise<{ locale: string }> }) {
  const { locale: raw } = await params;
  if (!isLocale(raw)) notFound();
  const locale = raw as Locale;
  const dict = getDictionary(locale);

  const values = [
    { title: dict.story.value1Title, text: dict.story.value1Text },
    { title: dict.story.value2Title, text: dict.story.value2Text },
    { title: dict.story.value3Title, text: dict.story.value3Text },
  ];

  return (
    <>
      <section className="relative overflow-hidden bg-charcoal">
        <div className="absolute inset-0">
          <Image src="/images/hero-main.jpg" alt={dict.story.title} fill priority sizes="100vw" className="object-cover opacity-55" />
        </div>
        <div className="shell relative flex min-h-[440px] flex-col justify-end py-14 text-ivory md:min-h-[560px] md:py-20">
          <p className="eyebrow text-gold">{dict.story.eyebrow}</p>
          <h1 className="mt-4 max-w-3xl font-serif text-5xl font-light leading-[1.02] tracking-tight md:text-7xl">
            {dict.story.title}
          </h1>
          <p className="mt-5 max-w-xl text-[15px] leading-relaxed text-ivory/65">{dict.story.subtitle}</p>
        </div>
      </section>

      <section className="shell py-14 md:py-20">
        <div className="grid gap-10 lg:grid-cols-[0.72fr_1.28fr] lg:gap-16">
          <div>
            <p className="font-serif text-4xl font-light leading-tight text-charcoal md:text-5xl">
              « {dict.story.p3.split(":")[0]} »
            </p>
            <p className="mt-5 text-[12px] uppercase tracking-[0.18em] text-charcoal/35">DAR ZELLIGE · 1998</p>
          </div>
          <div className="max-w-2xl space-y-5 text-[15px] leading-[1.95] text-charcoal/65">
            <p className="first-letter:float-left first-letter:mr-2 first-letter:font-serif first-letter:text-6xl first-letter:leading-[0.8] first-letter:text-charcoal">
              {dict.story.p1}
            </p>
            <p>{dict.story.p2}</p>
            <p>{dict.story.p3}</p>
          </div>
        </div>
      </section>

      <section className="border-y border-border bg-cream/50">
        <div className="shell grid items-center gap-8 py-12 md:grid-cols-2 md:py-16">
          <div className="relative aspect-[4/5] overflow-hidden bg-sand/20">
            <Image
              src="/images/story-clay.jpg"
              alt={dict.story.imageCaption}
              fill
              sizes="(max-width: 768px) 100vw, 45vw"
              className="object-cover"
            />
          </div>
          <div className="md:pl-8">
            <p className="eyebrow">{dict.product.craft}</p>
            <h2 className="mt-4 font-serif text-3xl font-light leading-tight md:text-4xl">
              {dict.story.value2Title}
            </h2>
            <p className="mt-4 max-w-lg text-[15px] leading-[1.9] text-charcoal/60">{dict.story.value2Text}</p>
            <p className="mt-4 max-w-lg text-[15px] leading-[1.9] text-charcoal/60">{dict.story.value3Text}</p>
            <p className="mt-6 text-[12px] uppercase tracking-[0.16em] text-charcoal/35">{dict.story.imageCaption}</p>
          </div>
        </div>
      </section>

      <section className="shell py-14 md:py-20">
        <div className="flex flex-col justify-between gap-4 md:flex-row md:items-end">
          <div>
            <p className="eyebrow">{dict.story.eyebrow}</p>
            <h2 className="mt-3 font-serif text-3xl font-light md:text-4xl">{dict.story.valuesTitle}</h2>
          </div>
          <p className="max-w-md text-[13px] leading-relaxed text-charcoal/45">{dict.footer.description}</p>
        </div>
        <div className="mt-10 grid gap-px overflow-hidden border border-border bg-border md:grid-cols-3">
          {values.map((value, index) => (
            <div key={value.title} className="bg-ivory p-6 md:p-8">
              <p className="font-serif text-sm text-gold">0{index + 1}</p>
              <h3 className="mt-4 font-serif text-2xl font-light">{value.title}</h3>
              <p className="mt-3 text-[14px] leading-[1.85] text-charcoal/55">{value.text}</p>
            </div>
          ))}
        </div>
      </section>

      <section className="relative overflow-hidden bg-charcoal py-16 text-ivory md:py-20">
        <div className="shell flex flex-col items-center text-center">
          <p className="eyebrow text-gold">{dict.story.eyebrow}</p>
          <h2 className="mt-4 max-w-2xl font-serif text-3xl font-light leading-tight md:text-5xl">
            {dict.story.subtitle}
          </h2>
          <Button asChild variant="outline" className="mt-8 border-ivory/30 text-ivory hover:bg-ivory hover:text-charcoal">
            <Link href={`/${locale}/shop`}>
              {dict.story.cta}
              <ArrowRight className="h-4 w-4 rtl:rotate-180" />
            </Link>
          </Button>
        </div>
      </section>
    </>
  );
}
