import { NextRequest, NextResponse } from 'next/server' import { supabaseAdmin } from '@/lib/supabase/admin' import jsPDF from 'jspdf' import { format } from 'date-fns' import { es } from 'date-fns/locale' import { Resend } from 'resend' const resend = new Resend(process.env.RESEND_API_KEY!) /** @description Send receipt email for booking */ export async function POST( request: NextRequest, { params }: { params: { bookingId: string } } ) { try { // Get booking data const { data: booking, error: bookingError } = await supabaseAdmin .from('bookings') .select(` *, customer:customers(*), service:services(*), staff:staff(*), location:locations(*) `) .eq('id', params.bookingId) .single() if (bookingError || !booking) { return NextResponse.json({ error: 'Booking not found' }, { status: 404 }) } // Generate PDF const doc = new jsPDF() doc.setFont('helvetica') // Header doc.setFontSize(20) doc.setTextColor(139, 69, 19) doc.text('ANCHOR:23', 20, 30) doc.setFontSize(14) doc.setTextColor(0, 0, 0) doc.text('Recibo de Reserva', 20, 45) // Details doc.setFontSize(12) let y = 65 doc.text(`Número de Reserva: ${booking.id.slice(-8).toUpperCase()}`, 20, y) y += 10 doc.text(`Cliente: ${booking.customer.first_name} ${booking.customer.last_name}`, 20, y) y += 10 doc.text(`Servicio: ${booking.service.name}`, 20, y) y += 10 doc.text(`Fecha y Hora: ${format(new Date(booking.date), 'PPP p', { locale: es })}`, 20, y) y += 10 doc.text(`Total: $${booking.service.price} MXN`, 20, y) // Footer y = 250 doc.setFontSize(10) doc.setTextColor(128, 128, 128) doc.text('ANCHOR:23 - Belleza anclada en exclusividad', 20, y) const pdfBuffer = Buffer.from(doc.output('arraybuffer')) // Send email const emailHtml = `
Hola ${booking.customer.first_name},
Tu reserva ha sido confirmada. Adjunto el recibo.
Servicio: ${booking.service.name}
Fecha: ${format(new Date(booking.date), 'PPP', { locale: es })}
Hora: ${format(new Date(booking.date), 'p', { locale: es })}
Total: $${booking.service.price} MXN