import Link from "next/link";
import { getOffers } from "@/lib/offersStore";
import { formatPrice } from "@/lib/formatPrice";
import { AdminNav } from "@/components/admin/AdminNav";

export const dynamic = "force-dynamic";

const STATUS_STYLES: Record<string, string> = {
  draft: "bg-ink/10 text-ink/70",
  scheduled: "bg-olive/15 text-olive",
  live: "bg-tomato/15 text-tomato",
  paused: "bg-yellow-500/15 text-yellow-700",
  expired: "bg-ink/10 text-ink/50",
};

export default async function AdminOffersPage() {
  const offers = await getOffers();

  return (
    <div>
      <AdminNav />
      <div className="mx-auto max-w-5xl px-6 py-10">
        <div className="flex items-center justify-between">
          <h1 className="text-2xl font-semibold">Offers</h1>
          <Link
            href="/admin/offers/new"
            className="rounded-lg bg-tomato px-4 py-2 text-sm font-medium text-white hover:bg-tomato-dark"
          >
            + New offer
          </Link>
        </div>

        <div className="mt-6 overflow-hidden rounded-xl border border-ink/10 bg-white">
          <table className="w-full text-left text-sm">
            <thead className="bg-ink/5 text-xs uppercase tracking-wide text-ink/50">
              <tr>
                <th className="px-4 py-3">Title</th>
                <th className="px-4 py-3">Status</th>
                <th className="px-4 py-3">Price</th>
                <th className="px-4 py-3">Vouchers</th>
                <th className="px-4 py-3">Dates</th>
                <th className="px-4 py-3" />
              </tr>
            </thead>
            <tbody className="divide-y divide-ink/10">
              {offers.map((offer) => (
                <tr key={offer.id}>
                  <td className="px-4 py-3 font-medium">{offer.title.en}</td>
                  <td className="px-4 py-3">
                    <span className={`rounded-full px-2.5 py-1 text-xs font-medium ${STATUS_STYLES[offer.status]}`}>
                      {offer.status}
                    </span>
                  </td>
                  <td className="px-4 py-3">
                    €{formatPrice(offer.offerPrice)} <span className="text-ink/40 line-through">€{formatPrice(offer.normalPrice)}</span>
                  </td>
                  <td className="px-4 py-3">{offer.vouchersSold} / {offer.maxVouchers}</td>
                  <td className="px-4 py-3 text-ink/60">{offer.startDate} → {offer.endDate}</td>
                  <td className="px-4 py-3 text-right">
                    <Link href={`/admin/offers/${offer.id}`} className="text-tomato hover:underline">
                      Edit
                    </Link>
                  </td>
                </tr>
              ))}
              {offers.length === 0 && (
                <tr>
                  <td colSpan={6} className="px-4 py-8 text-center text-ink/50">No offers yet.</td>
                </tr>
              )}
            </tbody>
          </table>
        </div>
      </div>
    </div>
  );
}
