"use client";

import { useEffect, useState } from "react";
import { useTranslations } from "next-intl";
import { Link } from "@/i18n/navigation";
import { formatPrice } from "@/lib/formatPrice";
import { Texture } from "@/components/Texture";

const DISMISSED_KEY = "offerPopupDismissed";
const EXIT_DURATION = 250;

export function OfferPopup({
  offerId,
  title,
  description,
  normalPrice,
  offerPrice,
}: {
  offerId: string;
  title: string;
  description: string;
  normalPrice: number;
  offerPrice: number;
}) {
  const t = useTranslations("offerPopup");
  const [visible, setVisible] = useState(false);
  const [closing, setClosing] = useState(false);

  useEffect(() => {
    if (sessionStorage.getItem(DISMISSED_KEY) === offerId) return;
    const timer = setTimeout(() => setVisible(true), 1200);
    return () => clearTimeout(timer);
  }, [offerId]);

  useEffect(() => {
    if (!visible) return;
    function onKey(e: KeyboardEvent) {
      if (e.key === "Escape") dismiss();
    }
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, [visible]);

  function dismiss() {
    sessionStorage.setItem(DISMISSED_KEY, offerId);
    setClosing(true);
    setTimeout(() => setVisible(false), EXIT_DURATION);
  }

  if (!visible) return null;

  const discount = Math.round((1 - offerPrice / normalPrice) * 100);

  return (
    <div className="fixed inset-0 z-[100] flex items-center justify-center p-5" role="dialog" aria-modal="true">
      <div
        className={`absolute inset-0 bg-ink/75 backdrop-blur-md ${closing ? "opacity-0 transition-opacity duration-200" : "animate-backdrop-in"}`}
        onClick={dismiss}
      />

      <div
        className={`relative w-full max-w-sm overflow-hidden rounded-[1.75rem] border border-brass/20 bg-espresso shadow-[0_30px_80px_-20px_rgba(0,0,0,0.7)] ${
          closing ? "animate-popup-out" : "animate-popup-in"
        }`}
      >
        {/* Ambient warm glow drifting behind the texture, echoing the Hero treatment. */}
        <div
          className="animate-glow-drift pointer-events-none absolute -top-16 -right-10 h-56 w-56 rounded-full blur-3xl"
          style={{ background: "radial-gradient(circle, rgba(193,114,64,0.55), transparent 70%)" }}
        />
        <div
          className="animate-glow-drift pointer-events-none absolute -bottom-16 -left-10 h-56 w-56 rounded-full blur-3xl"
          style={{ background: "radial-gradient(circle, rgba(31,92,63,0.35), transparent 70%)", animationDelay: "-3.5s" }}
        />
        <Texture variant="dark" />
        <div className="tricolor-rule relative z-10" />

        {discount > 0 && (
          <div className="pointer-events-none absolute -left-12 top-6 z-10 w-40 -rotate-45 overflow-hidden">
            <div className="relative bg-tomato py-1.5 text-center shadow-[0_4px_14px_rgba(0,0,0,0.35)]">
              <span className="eyebrow text-[10px] text-ivory">−{discount}%</span>
              <span
                className="absolute inset-0 -translate-x-full"
                style={{
                  background: "linear-gradient(100deg, transparent, rgba(255,255,255,0.45), transparent)",
                  animation: "ribbon-shine 2.6s ease-in-out infinite",
                  animationDelay: "1s",
                }}
              />
            </div>
          </div>
        )}

        <button
          onClick={dismiss}
          aria-label={t("close")}
          className="group absolute right-4 top-5 z-20 flex h-8 w-8 items-center justify-center rounded-full border border-ivory/15 text-ivory-dim transition hover:border-ivory/40 hover:text-ivory"
        >
          <span className="transition-transform duration-300 group-hover:rotate-90">✕</span>
        </button>

        <div className="relative z-10 p-7 pt-16">
          <p className="eyebrow flex items-center gap-2 text-brass">
            <span className="h-1.5 w-1.5 rounded-full bg-tomato" />
            {t("eyebrow")}
          </p>
          <h2 className="mt-3 max-w-[85%] font-serif text-[1.75rem] italic leading-tight text-ivory">{title}</h2>
          <p className="mt-2.5 text-sm leading-relaxed text-ivory-dim">{description}</p>

          <div className="mt-6 flex items-baseline gap-3 border-t border-ivory/10 pt-5">
            <span className="text-sm text-ivory-dim/50 line-through">€{formatPrice(normalPrice)}</span>
            <span className="font-serif text-4xl italic text-ivory">€{formatPrice(offerPrice)}</span>
          </div>

          <div className="mt-6 flex flex-wrap gap-3">
            <Link
              href={{ pathname: "/reserve", query: { offer: title } }}
              onClick={dismiss}
              className="eyebrow rounded-full bg-tomato px-6 py-3 text-ivory shadow-[0_10px_30px_-10px_rgba(193,69,42,0.7)] transition hover:-translate-y-0.5 hover:bg-tomato-dark"
            >
              {t("book")}
            </Link>
            <Link
              href="/offers"
              onClick={dismiss}
              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"
            >
              {t("details")}
            </Link>
          </div>
        </div>
      </div>
    </div>
  );
}
