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

function required(value: FormDataEntryValue | null) {
  return typeof value === "string" && value.trim().length > 0;
}

export async function POST(request: Request) {
  const form = await request.formData();
  const name = form.get("name");
  const email = form.get("email");
  const manufacturer = form.get("manufacturer");
  const model = form.get("model");
  const deviceType = form.get("deviceType");
  const description = form.get("description");
  const privacy = form.get("privacy");

  if (!required(name) || !required(email) || !required(manufacturer) || !required(model) || !required(deviceType) || !required(description) || privacy !== "on") {
    return NextResponse.json({ message: "Bitte füllen Sie alle Pflichtfelder aus." }, { status: 400 });
  }

  await addRepairInquiry(form);

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