import { NextResponse } from "next/server";
import { prisma } from "@/lib/db";

/**
 * Public endpoint — returns appointment details (no auth required).
 * Used by the confirmation page to display appointment info.
 */
export async function GET(
  _req: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;

    const appointment = await prisma.appointment.findUnique({
      where: { id },
      include: { lead: true, treatment: true, clinic: true },
    });

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

    return NextResponse.json({
      id: appointment.id,
      status: appointment.status,
      dateTime: appointment.dateTime,
      treatment: appointment.treatment.name,
      duration: appointment.treatment.duration,
      clinic: appointment.clinic.name,
      patientName: appointment.lead.name,
    });
  } catch (error) {
    console.error("Confirm details error:", error);
    return NextResponse.json({ error: "Something went wrong" }, { status: 500 });
  }
}
