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

export async function OffersGrid() {
  const t = await getTranslations("offers");
  const locale = await getLocale();
  const offers = await getOffers();
  const visible = offers.filter((o) => o.status === "live" || o.status === "scheduled").slice(0, 3);

  return (
    <section className="mx-auto max-w-6xl px-6 py-28">
      <Reveal className="flex items-end justify-between gap-4">
        <div>
          <p className="eyebrow text-brass">01</p>
          <h2 className="mt-3 font-serif text-4xl italic text-ivory">{t("title")}</h2>
          <p className="mt-3 max-w-lg text-ivory-dim">{t("subtitle")}</p>
        </div>
        <Link href="/offers" className="eyebrow hidden text-brass underline decoration-brass/40 underline-offset-4 transition hover:text-ivory md:inline">
          {t("viewAll")}
        </Link>
      </Reveal>

      <div className="mt-12 grid gap-6 md:grid-cols-3">
        {visible.map((offer, i) => {
          const spotsLeft = offer.maxVouchers - offer.vouchersSold;
          const soldOut = spotsLeft <= 0;
          return (
            <Reveal key={offer.id} delay={i * 100}>
              <article className="group flex h-full flex-col overflow-hidden rounded-2xl border border-ivory/10 bg-espresso-light transition hover:border-brass/30">
                <div className="tricolor-rule" />
                {offer.photo && (
                  <img src={offer.photo} alt="" loading="lazy" className="h-40 w-full object-cover" />
                )}
                <div className="flex flex-1 flex-col p-7">
                <div className="flex items-center justify-between">
                  <span className="eyebrow text-tomato">{offer.status === "scheduled" ? "Soon" : t("spotsLeft", { count: spotsLeft })}</span>
                  <span className="text-sm text-ivory-dim">
                    <s className="text-ivory-dim/40">€{formatPrice(offer.normalPrice)}</s>{" "}
                    <span className="text-lg font-semibold text-ivory">€{formatPrice(offer.offerPrice)}</span>
                  </span>
                </div>
                <h3 className="mt-4 font-serif text-2xl italic text-ivory">{localize(offer.title, locale)}</h3>
                <p className="mt-2 flex-1 text-sm leading-relaxed text-ivory-dim">{localize(offer.description, locale)}</p>
                <Link
                  href="/offers"
                  aria-disabled={soldOut}
                  className={`eyebrow mt-6 inline-block rounded-full px-5 py-2.5 text-center transition ${
                    soldOut
                      ? "bg-ivory/10 text-ivory-dim"
                      : "bg-tomato text-ivory group-hover:bg-tomato-dark"
                  }`}
                >
                  {soldOut ? t("soldOut") : t("book")}
                </Link>
                </div>
              </article>
            </Reveal>
          );
        })}
      </div>
    </section>
  );
}
