import { NextResponse } from 'next/server';
import { z } from 'zod';
import { allow } from '@/lib/rate-limit';

const ContactSchema = z.object({
  name: z.string().min(2),
  phone: z.string().min(7),
  email: z.string().email(),
  address: z.string().optional(),
  projectType: z.string().min(2),
  urgency: z.string().min(2),
  description: z.string().min(10)
});

export async function POST(req: Request) {
  if (!allow('contact', req.headers.get('x-forwarded-for') || 'local')) {
    return NextResponse.json({ error: 'Too many requests' }, { status: 429 });
  }
  const body = await req.json().catch(() => null);
  const parsed = ContactSchema.safeParse(body);
  if (!parsed.success) return NextResponse.json({ error: 'Invalid payload' }, { status: 400 });
  // TODO: integrate email/CRM
  console.log('Contact submission', parsed.data);
  return NextResponse.json({ ok: true });
}