import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { parseExperience } 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 items = await prisma.workExperience.findMany({
      orderBy: [{ sortIndex: "asc" }, { fromDate: "desc" }],
    });
    return NextResponse.json(items);
  });
}

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