import { z } from "zod";
import prisma from "@/server/db/prisma";
import { parseJson } from "@/server/http/parse";
import { ok } from "@/server/http/response";
import { withErrorHandling } from "@/server/http/withErrorHandling";

export const GET = withErrorHandling(async () => {
  const cards = await prisma.contactcard.findMany();
  return ok(cards);
});

const ContactCardCreateSchema = z.object({
  name: z.string().min(1),
  phone: z.string().min(1),
  email: z.string().email(),
});

export const POST = withErrorHandling(async (request: Request) => {
  const data = await parseJson(request, ContactCardCreateSchema);
  const card = await prisma.contactcard.create({
    data: { name: data.name, phone: data.phone, email: data.email },
  });
  return ok(card);
});
