import { saveContact } from "@/lib/contactStore";
import { contactSchema } from "@/schemas/schemas";

export async function POST(req: Request) {
  try {
    const body = await req.json();
    const {
      firstName,
      lastName,
      companyName,
      address,
      email,
      phone,
      industry,
      notes,
      subscribe,
    } = contactSchema.parse(body);

    const saved = await saveContact({
      firstName,
      lastName,
      companyName,
      address,
      email,
      phone,
      industry,
      notes,
      subscribe,
    });

    return Response.json({ success: true, id: saved.id });
  } catch (err: any) {
    if (err?.issues) {
      return Response.json(
        { success: false, errors: err.issues },
        { status: 400 }
      );
    }
    return Response.json({ success: false }, { status: 500 });
  }
}
