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

export default async function AdminCategoriesPage() {
  const session = await getServerSession()

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

  const categories = await prisma.category.findMany({
    include: { _count: { select: { agents: true } } },
    orderBy: { createdAt: "desc" },
  })

  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>
          <div className="flex items-center gap-4">
            <Link href="/admin" className="text-sm text-gray-600 hover:text-gray-900">
              返回仪表盘
            </Link>
            <Link href="/admin/categories/new" className="bg-blue-600 text-white px-4 py-2 rounded-lg text-sm hover:bg-blue-700">
              添加分类
            </Link>
          </div>
        </div>
      </nav>

      <main className="max-w-7xl mx-auto px-6 py-8">
        <div className="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
          <table className="w-full">
            <thead className="bg-gray-50 border-b border-gray-200">
              <tr>
                <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">名称</th>
                <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">图标</th>
                <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">智能体数量</th>
                <th className="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">操作</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-gray-200">
              {categories.map((cat) => (
                <tr key={cat.id} className="hover:bg-gray-50">
                  <td className="px-6 py-4">
                    <div className="font-medium text-gray-900">{cat.name}</div>
                    <div className="text-sm text-gray-500">{cat.description}</div>
                  </td>
                  <td className="px-6 py-4 text-2xl">
                    {cat.icon || "-"}
                  </td>
                  <td className="px-6 py-4 text-sm text-gray-600">
                    {cat._count.agents}
                  </td>
                  <td className="px-6 py-4 text-right">
                    <div className="flex items-center justify-end gap-2">
                      <Link
                        href={`/admin/categories/${cat.id}/edit`}
                        className="text-blue-600 hover:text-blue-700 text-sm"
                      >
                        编辑
                      </Link>
                      <DeleteCategoryButton id={cat.id} />
                    </div>
                  </td>
                </tr>
              ))}
            </tbody>
          </table>

          {categories.length === 0 && (
            <div className="text-center py-12 text-gray-500">
              暂无分类，<Link href="/admin/categories/new" className="text-blue-600">点击添加</Link>
            </div>
          )}
        </div>
      </main>
    </div>
  )
}
