import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { formatMoney, formatDuration } from "@/lib/format";

export async function GET(
  _request: Request,
  ctx: RouteContext<"/api/booking/session/[sessionId]">
) {
  const { sessionId } = await ctx.params;

  const booking = await prisma.booking.findUnique({
    where: { stripeSessionId: sessionId },
    include: { service: true },
  });

  if (!booking) {
    return NextResponse.json({ error: "Booking not found" }, { status: 404 });
  }

  return NextResponse.json({
    status: booking.status,
    customerName: booking.customerName,
    date: booking.date,
    startTime: booking.startTime,
    serviceName: booking.service.name,
    duration: formatDuration(booking.service.durationMinutes),
    amount: formatMoney(booking.amountPaidCents ?? booking.service.priceCents),
  });
}
