import { NextResponse } from "next/server";

import { requireApiAccess } from "@/lib/authorization";
import { prisma } from "@/lib/prisma";

export async function GET(request: Request) {
  const auth = requireApiAccess(request);
  if (!auth.ok) return auth.response;

  const reasons = await prisma.adjustmentReason.findMany({
    where: { status: "ACTIVE" },
    orderBy: [{ category: "asc" }, { name: "asc" }]
  });

  return NextResponse.json({
    data: {
      adjustment_reasons: reasons.map((reason) => ({
        id: reason.id.toString(),
        code: reason.code,
        name: reason.name,
        description: reason.description,
        category: reason.category
      }))
    }
  });
}
