import { NextResponse } from 'next/server'; import { supabaseAdmin } from '@/lib/supabase'; import { STYLES } from '@/lib/types'; export async function POST(req: Request){ try{ const form=await req.formData(); const email=String(form.get('email')||''); const name=String(form.get('name')||''); const style=String(form.get('style')||''); if(!email||!name||!STYLES.includes(style as any)) return NextResponse.json({error:'Ongeldige invoer.'},{status:400}); const files=form.getAll('photos').filter(Boolean) as File[]; if(files.length<3||files.length>6) return NextResponse.json({error:'Upload 3 tot 6 foto’s.'},{status:400}); const sb=supabaseAdmin(); const {data:order,error}=await sb.from('orders').insert({email,name,style,product:String(form.get('product')||'bruidspaar-taarttopper'),wedding_date:String(form.get('wedding_date')||''),notes:String(form.get('notes')||''),status:'new',payment_status:'pending'}).select('id').single(); if(error) throw error; const bucket=process.env.SUPABASE_BUCKET || 'order-uploads'; for(const file of files){ const path=`${order.id}/${crypto.randomUUID()}-${file.name}`; const buffer=Buffer.from(await file.arrayBuffer()); const up=await sb.storage.from(bucket).upload(path, buffer, {contentType:file.type, upsert:false}); if(up.error) throw up.error; const {data:pub}=sb.storage.from(bucket).getPublicUrl(path); await sb.from('uploads').insert({order_id:order.id,image_url:pub.publicUrl,path}); } return NextResponse.json({orderId:order.id}); }catch(e:any){return NextResponse.json({error:e.message||'Serverfout'},{status:500})} }