import { redirect } from "next/navigation";
import { getCurrentUser } from "@/lib/auth-utils";
import prisma from "@/lib/db";
import { BookingForm } from "./booking-form";

interface BookingPageProps {
  searchParams: Promise<{ tourId?: string; slug?: string }>;
}

export default async function NewBookingPage({ searchParams }: BookingPageProps) {
  const user = await getCurrentUser();
  const params = await searchParams;

  if (!user) {
    redirect("/login");
  }

  if (!params.tourId) {
    redirect("/");
  }

  const tour = await prisma.tour.findUnique({
    where: { id: params.tourId },
    include: {
      createdBy: {
        select: {
          name: true,
          email: true,
        },
      },
    },
  });

  if (!tour) {
    redirect("/");
  }

  return <BookingForm tour={tour} user={user} />;
}
