import { NextResponse } from "next/server";
import { addContactInquiry } from "@/lib/admin/store";

function text(value: FormDataEntryValue | null) {
  return typeof value === "string" ? value.trim() : "";
}

export async function POST(request: Request) {
  const form = await request.formData();
  const name = text(form.get("name"));
  const email = text(form.get("email"));
  const subject = text(form.get("subject"));
  const message = text(form.get("message"));

  if (!name || !email.includes("@") || !subject || message.length < 10 || form.get("privacy") !== "on") {
    return NextResponse.json({ message: "Bitte füllen Sie alle Pflichtfelder aus." }, { status: 400 });
  }

  await addContactInquiry(form);

  return NextResponse.json({
    message: "Ihre Nachricht wurde erfasst. Sie erhalten eine Rückmeldung.",
  });
}
