import { getServerSession } from "next-auth/next"
import { redirect } from "next/navigation"
import Link from "next/link"
import { prisma } from "@/app/lib/prisma"
import CategoryForm from "../../CategoryForm"

export default async function EditCategoryPage({
  params,
}: {
  params: Promise<{ id: string }>
}) {
  const session = await getServerSession()

  if (!session) {
    redirect("/admin/login")
  }

  const { id } = await params
  const category = await prisma.category.findUnique({
    where: { id: parseInt(id) },
  })

  if (!category) {
    redirect("/admin/categories")
  }

  return (
    <div className="min-h-screen bg-gray-50">
      <nav className="bg-white border-b border-gray-200">
        <div className="max-w-7xl mx-auto px-6 py-4 flex items-center justify-between">
          <h1 className="text-xl font-bold text-gray-900">编辑分类</h1>
          <Link href="/admin/categories" className="text-sm text-gray-600 hover:text-gray-900">
            返回列表
          </Link>
        </div>
      </nav>

      <main className="max-w-3xl mx-auto px-6 py-8">
        <div className="bg-white rounded-2xl shadow-sm border border-gray-200 p-6">
          <CategoryForm
            category={{
              id: category.id,
              name: category.name,
              icon: category.icon || "",
              description: category.description || "",
            }}
          />
        </div>
      </main>
    </div>
  )
}
