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

export default async function OffersPage() {
  const t = await getTranslations("offers");
  const locale = await getLocale();
  const offers = await getOffers();
  const visible = offers.filter((o) => o.status === "live" || o.status === "scheduled");

  return (
    <div className="mx-auto max-w-3xl px-6 py-24">
      <Reveal className="text-center">
        <p className="eyebrow text-brass">La Tavola Italiana</p>
        <h1 className="mt-4 font-serif text-5xl italic text-ivory">{t("title")}</h1>
        <p className="mt-3 text-ivory-dim">{t("subtitle")}</p>
      </Reveal>

      <div className="mt-16 space-y-6">
        {visible.map((offer, i) => {
          const spotsLeft = offer.maxVouchers - offer.vouchersSold;
          const soldOut = spotsLeft <= 0;
          const weekdayLabel = offer.validWeekdays
            .map((d) => ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d])
            .join(", ");

          return (
            <Reveal key={offer.id} delay={i * 100}>
              <article className="overflow-hidden rounded-2xl bg-espresso-light">
                <div className="tricolor-rule" />
                {offer.photo && (
                  <img src={offer.photo} alt="" loading="lazy" className="h-56 w-full object-cover" />
                )}
                <div className="p-7">
                <div className="flex flex-wrap items-start justify-between gap-4">
                  <div>
                    <h2 className="font-serif text-2xl italic text-ivory">{localize(offer.title, locale)}</h2>
                    <p className="mt-2 max-w-xl text-sm leading-relaxed text-ivory-dim">{localize(offer.description, locale)}</p>
                  </div>
                  <div className="text-right">
                    <p className="text-sm text-ivory-dim/50 line-through">€{formatPrice(offer.normalPrice)}</p>
                    <p className="text-3xl font-semibold text-ivory">€{formatPrice(offer.offerPrice)}</p>
                  </div>
                </div>

                <dl className="mt-6 grid grid-cols-2 gap-x-6 gap-y-3 border-t border-ivory/10 pt-5 text-sm text-ivory-dim sm:grid-cols-4">
                  <div><dt className="eyebrow text-ivory-dim/40">Days</dt><dd className="mt-1">{weekdayLabel}</dd></div>
                  {offer.slotStart && (
                    <div><dt className="eyebrow text-ivory-dim/40">Time</dt><dd className="mt-1 tabular-nums">{offer.slotStart}–{offer.slotEnd}</dd></div>
                  )}
                  <div><dt className="eyebrow text-ivory-dim/40">Group size</dt><dd className="mt-1">{offer.minGroupSize}–{offer.maxGroupSize}</dd></div>
                  <div><dt className="eyebrow text-ivory-dim/40">Availability</dt><dd className="mt-1">{spotsLeft}/{offer.maxVouchers}</dd></div>
                </dl>

                <BookOfferButton
                  offerTitle={localize(offer.title, locale)}
                  soldOut={soldOut}
                  label={soldOut ? t("soldOut") : t("book")}
                />
                </div>
              </article>
            </Reveal>
          );
        })}
      </div>
    </div>
  );
}
