import { getReservations } from "@/lib/reservationsStore";
import { AdminNav } from "@/components/admin/AdminNav";

export const dynamic = "force-dynamic";

const STATUS_STYLES: Record<string, string> = {
  sent: "bg-olive/15 text-olive",
  failed: "bg-tomato/15 text-tomato",
  not_configured: "bg-ink/10 text-ink/50",
};

export default async function AdminReservationsPage() {
  const reservations = await getReservations();

  return (
    <div>
      <AdminNav />
      <div className="mx-auto max-w-5xl px-6 py-10">
        <h1 className="text-2xl font-semibold">Reservation requests</h1>
        <p className="mt-1 text-sm text-ink/60">{reservations.length} requests received</p>

        <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">Received</th>
                <th className="px-4 py-3">Name</th>
                <th className="px-4 py-3">Contact</th>
                <th className="px-4 py-3">Guests</th>
                <th className="px-4 py-3">Date / time</th>
                <th className="px-4 py-3">Notes</th>
                <th className="px-4 py-3">Email</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-ink/10">
              {reservations.map((r) => (
                <tr key={r.id}>
                  <td className="px-4 py-3 whitespace-nowrap text-ink/60">
                    {new Date(r.createdAt).toLocaleString("en-GB")}
                  </td>
                  <td className="px-4 py-3 font-medium">{r.name}</td>
                  <td className="px-4 py-3">
                    <div>{r.phone}</div>
                    {r.email && <div className="text-ink/50">{r.email}</div>}
                  </td>
                  <td className="px-4 py-3">{r.guests}</td>
                  <td className="px-4 py-3 whitespace-nowrap">{r.date} {r.time}</td>
                  <td className="px-4 py-3 max-w-xs text-ink/60">{r.notes}</td>
                  <td className="px-4 py-3">
                    <span className={`rounded-full px-2.5 py-1 text-xs font-medium ${STATUS_STYLES[r.emailStatus]}`}>
                      {r.emailStatus}
                    </span>
                  </td>
                </tr>
              ))}
              {reservations.length === 0 && (
                <tr>
                  <td colSpan={7} className="px-4 py-8 text-center text-ink/50">No reservation requests yet.</td>
                </tr>
              )}
            </tbody>
          </table>
        </div>
      </div>
    </div>
  );
}
