import { getLocale, getTranslations } from "next-intl/server";
import { Link } from "@/i18n/navigation";
import { Reveal } from "@/components/Reveal";
import { Texture } from "@/components/Texture";
import { OfferBanner } from "@/components/OfferBanner";
import { OfferPopup } from "@/components/OfferPopup";
import { getOffers } from "@/lib/offersStore";
import { getStoryPhotos } from "@/lib/storyPhotosStore";
import { localize } from "@/lib/localize";

export const dynamic = "force-dynamic";

export default async function StoryPage() {
  const t = await getTranslations("story");
  const tNav = await getTranslations("nav");
  const locale = await getLocale();
  const offers = await getOffers();
  const featured = offers.find((o) => o.status === "live");
  const photos = await getStoryPhotos();

  return (
    <div>
      {featured && (
        <OfferPopup
          offerId={featured.id}
          title={localize(featured.title, locale)}
          description={localize(featured.description, locale)}
          normalPrice={featured.normalPrice}
          offerPrice={featured.offerPrice}
        />
      )}
      <OfferBanner />

      <section className="relative flex h-[52vh] min-h-[360px] items-end overflow-hidden border-b border-brass/20 bg-espresso">
        <img
          src={photos.hero.url}
          alt={photos.hero.caption || t("interiorAlt")}
          className="absolute inset-0 h-full w-full object-cover"
          loading="eager"
        />
        <div
          className="absolute inset-0"
          style={{
            backgroundImage: "linear-gradient(180deg, rgba(42,22,13,0.15) 0%, rgba(42,22,13,0.55) 60%, rgba(42,22,13,0.92) 100%)",
          }}
        />
        <Texture variant="dark" />
        <Reveal className="relative z-10 mx-auto w-full max-w-3xl px-6 pb-10 text-center">
          <p className="eyebrow text-brass">La Tavola Italiana</p>
          <h1 className="mt-3 font-serif text-4xl italic text-ivory md:text-5xl">{t("title")}</h1>
        </Reveal>
      </section>

      <div className="mx-auto max-w-3xl px-6 py-16 text-center">
        <Reveal>
          <p className="mx-auto max-w-2xl text-lg leading-relaxed text-ivory-dim">{t("body")}</p>
        </Reveal>
      </div>

      {photos.gallery.length > 0 && (
        <div className="mx-auto max-w-6xl px-6 pb-20">
          <Reveal className="grid gap-5 sm:grid-cols-3">
            {photos.gallery.map((img, i) => (
              <Reveal key={img.id} delay={i * 100} className="group overflow-hidden rounded-2xl border border-ivory/10">
                <div className="aspect-[4/5] overflow-hidden">
                  <img
                    src={img.url}
                    alt={img.caption ?? ""}
                    loading="lazy"
                    className="h-full w-full object-cover transition duration-500 group-hover:scale-105"
                  />
                </div>
              </Reveal>
            ))}
          </Reveal>
        </div>
      )}

      <div className="border-t border-ivory/10 bg-espresso-light">
        <Reveal className="mx-auto flex max-w-3xl flex-col items-center gap-4 px-6 py-14 text-center">
          <p className="font-serif text-2xl italic text-ivory">{t("ctaTitle")}</p>
          <div className="flex flex-wrap justify-center gap-4">
            <Link
              href="/menu"
              className="eyebrow rounded-full border border-ivory/25 px-6 py-3 text-ivory transition hover:-translate-y-0.5 hover:border-ivory/60 hover:bg-ivory/5"
            >
              {tNav("menu")}
            </Link>
            <Link
              href="/reserve"
              className="eyebrow rounded-full bg-tomato px-6 py-3 text-ivory transition hover:-translate-y-0.5 hover:bg-tomato-dark"
            >
              {tNav("reserve")}
            </Link>
          </div>
        </Reveal>
      </div>
    </div>
  );
}
