import { verifySession } from "@/lib/dal";
import { prisma } from "@/lib/prisma";
import { BookingsView } from "@/components/admin/bookings-view";

export const dynamic = "force-dynamic";

export default async function AdminBookingsPage() {
  await verifySession();

  const bookings = await prisma.booking.findMany({
    orderBy: [{ date: "desc" }, { startTime: "desc" }],
    include: { service: true },
  });

  const viewBookings = bookings.map((booking) => ({
    id: booking.id,
    date: booking.date,
    startTime: booking.startTime,
    status: booking.status,
    customerName: booking.customerName,
    customerEmail: booking.customerEmail,
    customerPhone: booking.customerPhone,
    amountPaidCents: booking.amountPaidCents,
    serviceName: booking.service.name,
    servicePriceCents: booking.service.priceCents,
  }));

  return (
    <div>
      <h1 className="font-display text-3xl text-rosewood">Bookings</h1>
      <p className="mt-2 text-sm text-ink/60">
        {bookings.length} appointment{bookings.length === 1 ? "" : "s"} total.
      </p>

      <div className="mt-8">
        <BookingsView bookings={viewBookings} />
      </div>
    </div>
  );
}
