import Link from "next/link";
import { prisma } from "@/lib/db";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import { Plus } from "lucide-react";
import { DeleteAddonButton } from "@/components/admin/DeleteAddonButton";

export const dynamic = "force-dynamic";

export default async function AdminAddonsPage() {
  const addons = await prisma.addon.findMany({
    include: { _count: { select: { releases: true } } },
    orderBy: { updatedAt: "desc" },
  });

  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <h1 className="text-3xl font-bold">插件管理</h1>
        <Button render={<Link href="/admin/addons/new" />}>
          <Plus className="mr-2 h-4 w-4" />
          新建插件
        </Button>
      </div>

      <div className="rounded-lg border bg-card">
        <Table>
          <TableHeader>
            <TableRow>
              <TableHead>名称</TableHead>
              <TableHead>Slug</TableHead>
              <TableHead>分类</TableHead>
              <TableHead>状态</TableHead>
              <TableHead>版本数</TableHead>
              <TableHead>下载量</TableHead>
              <TableHead className="text-right">操作</TableHead>
            </TableRow>
          </TableHeader>
          <TableBody>
            {addons.length === 0 ? (
              <TableRow>
                <TableCell colSpan={7} className="text-center text-muted-foreground">
                  暂无插件，点击上方按钮创建
                </TableCell>
              </TableRow>
            ) : (
              addons.map((addon) => (
                <TableRow key={addon.id}>
                  <TableCell className="font-medium">{addon.name}</TableCell>
                  <TableCell className="text-muted-foreground">
                    {addon.slug}
                  </TableCell>
                  <TableCell>
                    <Badge variant="secondary">{addon.category}</Badge>
                  </TableCell>
                  <TableCell>
                    <Badge variant={addon.published ? "default" : "outline"}>
                      {addon.published ? "已发布" : "草稿"}
                    </Badge>
                  </TableCell>
                  <TableCell>{addon._count.releases}</TableCell>
                  <TableCell>{addon.totalDownloads}</TableCell>
                  <TableCell className="text-right">
                    <div className="flex justify-end gap-2">
                      <Button variant="outline" size="sm" render={<Link href={`/admin/addons/${addon.id}/edit`} />}>
                        编辑
                      </Button>
                      <DeleteAddonButton addonId={addon.id} addonName={addon.name} />
                    </div>
                  </TableCell>
                </TableRow>
              ))
            )}
          </TableBody>
        </Table>
      </div>
    </div>
  );
}
