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

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

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

  const agentsCount = await prisma.agent.count()
  const newsCount = await prisma.news.count()
  const categoriesCount = await prisma.category.count()

  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">
            <span className="text-sm text-gray-600">欢迎，{session.user?.name}</span>
            <Link
              href="/api/auth/signout"
              className="text-sm text-red-600 hover:text-red-700"
            >
              退出登录
            </Link>
          </div>
        </div>
      </nav>

      <main className="max-w-7xl mx-auto px-6 py-8">
        <div className="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
          <div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200">
            <div className="flex items-center gap-4 mb-4">
              <div className="w-12 h-12 bg-blue-100 rounded-xl flex items-center justify-center text-2xl">
                🤖
              </div>
              <div>
                <p className="text-sm text-gray-500">智能体总数</p>
                <p className="text-2xl font-bold text-gray-900">{agentsCount}</p>
              </div>
            </div>
            <Link href="/admin/agents" className="text-blue-600 text-sm hover:underline">
              管理智能体 →
            </Link>
          </div>

          <div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200">
            <div className="flex items-center gap-4 mb-4">
              <div className="w-12 h-12 bg-green-100 rounded-xl flex items-center justify-center text-2xl">
                📰
              </div>
              <div>
                <p className="text-sm text-gray-500">新闻总数</p>
                <p className="text-2xl font-bold text-gray-900">{newsCount}</p>
              </div>
            </div>
            <Link href="/admin/news" className="text-blue-600 text-sm hover:underline">
              管理新闻 →
            </Link>
          </div>

          <div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200">
            <div className="flex items-center gap-4 mb-4">
              <div className="w-12 h-12 bg-purple-100 rounded-xl flex items-center justify-center text-2xl">
                📂
              </div>
              <div>
                <p className="text-sm text-gray-500">分类总数</p>
                <p className="text-2xl font-bold text-gray-900">{categoriesCount}</p>
              </div>
            </div>
            <Link href="/admin/categories" className="text-blue-600 text-sm hover:underline">
              管理分类 →
            </Link>
          </div>
        </div>

        <div className="bg-white rounded-2xl shadow-sm border border-gray-200 p-6">
          <h2 className="text-lg font-semibold text-gray-900 mb-4">快捷操作</h2>
          <div className="flex flex-wrap gap-4">
            <Link
              href="/admin/agents/new"
              className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition"
            >
              添加智能体
            </Link>
            <Link
              href="/admin/news/new"
              className="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition"
            >
              添加新闻
            </Link>
            <Link
              href="/admin/categories/new"
              className="px-4 py-2 bg-purple-600 text-white rounded-lg hover:bg-purple-700 transition"
            >
              添加分类
            </Link>
            <Link
              href="/"
              className="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition"
            >
              查看前台
            </Link>
          </div>
        </div>
      </main>
    </div>
  )
}
