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";

// GET: Fetch the About section meta (image)
export const GET = withErrorHandling(async () => {
  const meta = await prisma.aboutsectionmeta.findFirst();
  return ok(meta);
});

// POST: Update the About section image
const AboutSectionMetaSchema = z.object({
  // DB column is non-nullable; use empty string to represent “no image”.
  image: z.string().optional(),
});

export const POST = withErrorHandling(async (request: Request) => {
  const { image } = await parseJson(request, AboutSectionMetaSchema);

  const updated = await prisma.aboutsectionmeta.upsert({
    where: { id: 1 },
    update: { image: image ?? "" },
    create: { image: image ?? "" },
  });

  return ok(updated);
});
