import { NextRequest, NextResponse } from "next/server"; export const runtime = "nodejs"; export async function POST(req: NextRequest) { try { const body = await req.json(); const name = (body.name || "").toString().trim(); const email = (body.email || "").toString().trim(); const message = (body.message || "").toString().trim(); const type = (body.type || "").toString().trim(); const domain = (body.domain || "").toString().trim(); const company = (body.company || "").toString().trim(); const concern = (body.concern || "").toString().trim(); const hosting = (body.hosting || "").toString().trim(); if (!name || !email || !message) return NextResponse.json({ ok: false, error: "Missing fields." }, { status: 400 }); const N8N_WEBHOOK_URL = "https://n8n.prestigepages.com/webhook/contact"; const N8N_WEBHOOK_SECRET = process.env.N8N_WEBHOOK_SECRET; if (!N8N_WEBHOOK_URL) return NextResponse.json({ ok: false, error: "Server not configured." }, { status: 500 }); // --- Send data to n8n --- const n8nResponse = await fetch(N8N_WEBHOOK_URL, { method: "POST", headers: { "Content-Type": "application/json", ...(N8N_WEBHOOK_SECRET ? { "x-secret-token": N8N_WEBHOOK_SECRET } : {}), }, body: JSON.stringify({ name, email, message, type, domain, company, concern, hosting, created_at: new Date().toISOString(), }), }); if (!n8nResponse.ok) { const text = await n8nResponse.text(); console.error("n8n webhook error:", text); return NextResponse.json({ ok: false, error: "n8n request failed." }, { status: 502 }); } // ✅ Success return NextResponse.json({ ok: true }); } catch (err) { console.error("Unexpected error:", err); return NextResponse.json({ ok: false, error: "Unexpected." }, { status: 500 }); } }