import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { parseProject } from "@/lib/dto";
import { guard, run, readBody } from "@/lib/adminRoute";

export async function GET() {
  const denied = await guard();
  if (denied) return denied;
  return run(async () => {
    const projects = await prisma.project.findMany({
      orderBy: [{ size: "asc" }, { name: "asc" }],
    });
    return NextResponse.json(projects);
  });
}

export async function POST(req: Request) {
  const denied = await guard();
  if (denied) return denied;
  return run(async () => {
    const data = parseProject(await readBody(req));
    const created = await prisma.project.create({ data });
    return NextResponse.json(created, { status: 201 });
  });
}
