import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { requireAdmin, unauthorized } from "@/lib/admin-api";

export async function GET() {
  const admin = await requireAdmin();
  if (!admin) return unauthorized();
  return NextResponse.json(
    await prisma.galleryImage.findMany({ orderBy: { sortOrder: "asc" } })
  );
}

export async function POST(req: Request) {
  const admin = await requireAdmin();
  if (!admin) return unauthorized();
  const body = await req.json();
  const g = await prisma.galleryImage.create({
    data: {
      title: body.title ?? null,
      imageUrl: body.imageUrl,
      year: Number(body.year) || 0,
      category: body.category ?? "PROFISSIONAL",
      sortOrder: Number(body.sortOrder) || 0,
    },
  });
  return NextResponse.json(g);
}
