import { NextRequest, NextResponse } from "next/server"
import { prisma } from "@//app/lib/prisma"

// 更新分类
export async function PUT(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    const data = await request.json()
    const category = await prisma.category.update({
      where: { id: parseInt(id) },
      data: {
        name: data.name,
        icon: data.icon || null,
        description: data.description || null,
      },
    })
    return NextResponse.json(category)
  } catch (error) {
    return NextResponse.json({ error: "更新失败" }, { status: 500 })
  }
}

// 删除分类
export async function DELETE(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    await prisma.category.delete({
      where: { id: parseInt(id) },
    })
    return NextResponse.json({ success: true })
  } catch (error) {
    return NextResponse.json({ error: "删除失败" }, { status: 500 })
  }
}
