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

interface Params { params: Promise<{ id: string }> }

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

export async function DELETE(_req: NextRequest, { params }: Params) {
  try {
    await requireAuth();
    const { id } = await params;
    await prisma.usesItem.delete({ where: { id } });
    return Response.json({ success: true });
  } catch (err) {
    console.error("[uses/DELETE]", err);
    return Response.json({ error: "Server error" }, { status: 500 });
  }
}
