import Link from "next/link";
import type { Metadata } from "next";
import { getOffers } from "@/lib/api";
import Countdown from "@/components/Countdown";
import ProductCard from "@/components/ProductCard";

export const revalidate = 120;

export const metadata: Metadata = {
  title: "Trade Offers — Monthly Deals, Bundles & Volume Discounts | Awafi Drugstore",
  description:
    "This month's trade offers for UAE pharmacies and clinics: monthly deals, bundles, volume discounts and near-expiry clearance from Awafi Drugstore.",
  alternates: { canonical: "/portal/offers" },
};

const ORDER = ["campaign", "monthly", "bundle", "volume", "clearance"];

export default async function OffersPage() {
  const { offers, deals } = await getOffers();
  const sorted = [...offers].sort((a, b) => ORDER.indexOf(a.type) - ORDER.indexOf(b.type));
  const campaigns = [...new Set(offers.map((o) => o.campaign).filter(Boolean))] as string[];

  return (
    <>
      <section className="pb-cathead pb-tone-sand">
        <div className="pb-shell">
          <nav className="pb-crumbs" aria-label="Breadcrumb">
            <Link href="/portal">Home</Link><span>/</span><span className="pb-crumbs-current">Trade Offers</span>
          </nav>
          <h1 className="pb-cathead-title">Trade Offers</h1>
          <p className="pb-cathead-sub">
            Monthly offers, bundles, volume discounts and near-expiry clearance for licensed pharmacies, clinics and retailers.
            {campaigns.length > 0 && <> Now on: <strong>{campaigns.join(" · ")}</strong>.</>}
          </p>
          {sorted.length > 0 && (
            <div className="pb-chips pb-brand-chips" style={{ marginTop: 16 }}>
              {sorted.map((o) => (
                <a key={o.slug} href={`#offer-${o.slug}`} className="pb-chip">{o.title}</a>
              ))}
            </div>
          )}
        </div>
      </section>

      {sorted.length === 0 && deals.length === 0 && (
        <div className="pb-shell">
          <div className="pb-empty">
            <p>No trade offers are running right now. Ask your account manager about volume pricing, or check back soon.</p>
            <Link href="/contact" className="pb-btn pb-btn--primary">Contact sales</Link>
          </div>
        </div>
      )}

      {sorted.map((o) => (
        <section className="pb-section pb-offer-section" id={`offer-${o.slug}`} key={o.slug}>
          <div className="pb-shell">
            <div className="pb-offer-head">
              <div>
                <span className="pb-section-kicker">{o.campaign || o.typeLabel}</span>
                <h2>
                  {o.title} {o.badge && <span className="pb-offer-badge">{o.badge}</span>}
                </h2>
                {o.summary && <p>{o.summary}</p>}
              </div>
              {o.ends && <Countdown ends={o.ends} />}
            </div>
            {o.products && o.products.length > 0 ? (
              <div className="pb-grid">
                {o.products.map((p) => (
                  <div className="pb-grid-cell" key={p.id}><ProductCard p={p} /></div>
                ))}
              </div>
            ) : (
              o.link && <Link href={o.link} className="pb-btn pb-btn--ghost">See the range</Link>
            )}
            {o.terms && <p className="pb-offer-terms"><strong>Terms:</strong> {o.terms}</p>}
          </div>
        </section>
      ))}

      {deals.length > 0 && (
        <section className="pb-section pb-section--tint" id="all-deals">
          <div className="pb-shell">
            <div className="pb-section-head">
              <span className="pb-section-kicker">Trade prices</span>
              <h2>Every line on offer</h2>
              <p>Products currently on a sale price or volume pricing (buy more, pay less).</p>
            </div>
            <div className="pb-grid">
              {deals.map((p) => (
                <div className="pb-grid-cell" key={p.id}><ProductCard p={p} /></div>
              ))}
            </div>
          </div>
        </section>
      )}
      <div className="pb-section-spacer" />
    </>
  );
}
