export const dynamic = 'force-dynamic';

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

export async function POST(request: Request) {
  try {
    const body = await request?.json?.();
    const { name, email, phone, company, service, preferredDate, preferredTime, notes } = body ?? {};

    if (!name || !email || !service || !preferredDate || !preferredTime) {
      return NextResponse.json({ error: 'Name, email, service, date, and time are required' }, { status: 400 });
    }

    const booking = await prisma.bookingRequest.create({
      data: {
        name: name ?? '',
        email: email ?? '',
        phone: phone ?? null,
        company: company ?? null,
        service: service ?? '',
        preferredDate: preferredDate ?? '',
        preferredTime: preferredTime ?? '',
        notes: notes ?? null,
      },
    });

    return NextResponse.json({ success: true, id: booking?.id ?? '' });
  } catch (error: any) {
    console.error('Booking submission error:', error);
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
  }
}
