import { type NextRequest } from "next/server";
import { prisma } from "@/lib/db";
import { requireAuth } from "@/lib/auth";

export async function GET() {
  try {
    await requireAuth();
    const items = await prisma.usesItem.findMany({
      orderBy: [{ category: "asc" }, { sortOrder: "asc" }],
    });
    return Response.json(items);
  } catch {
    return Response.json({ error: "Unauthorized" }, { status: 401 });
  }
}

export async function POST(req: NextRequest) {
  try {
    await requireAuth();
    const body = await req.json();
    const item = await prisma.usesItem.create({ data: body });
    return Response.json(item, { status: 201 });
  } catch (err) {
    console.error("[uses/POST]", err);
    return Response.json({ error: "Server error" }, { status: 500 });
  }
}
