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";

export const dynamic = "force-dynamic";

export default async function AdminReleasesPage() {
  const releases = await prisma.release.findMany({
    include: { addon: { select: { name: true, slug: true } } },
    orderBy: { createdAt: "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/releases/new" />}>
          <Plus className="mr-2 h-4 w-4" />
          发布新版本
        </Button>
      </div>

      <div className="rounded-lg border bg-card">
        <Table>
          <TableHeader>
            <TableRow>
              <TableHead>插件</TableHead>
              <TableHead>版本</TableHead>
              <TableHead>游戏版本</TableHead>
              <TableHead>下载方式</TableHead>
              <TableHead>下载量</TableHead>
              <TableHead>发布时间</TableHead>
              <TableHead>状态</TableHead>
            </TableRow>
          </TableHeader>
          <TableBody>
            {releases.length === 0 ? (
              <TableRow>
                <TableCell
                  colSpan={7}
                  className="text-center text-muted-foreground"
                >
                  暂无版本发布
                </TableCell>
              </TableRow>
            ) : (
              releases.map((release) => (
                <TableRow key={release.id}>
                  <TableCell className="font-medium">
                    {release.addon.name}
                  </TableCell>
                  <TableCell>v{release.version}</TableCell>
                  <TableCell className="text-muted-foreground">
                    {release.gameVersion || "-"}
                  </TableCell>
                  <TableCell>
                    <Badge variant="secondary">
                      {release.downloadType === "local" ? "本地文件" : "外部链接"}
                    </Badge>
                  </TableCell>
                  <TableCell>{release.downloadCount}</TableCell>
                  <TableCell className="text-muted-foreground">
                    {new Date(release.createdAt).toLocaleDateString("zh-CN")}
                  </TableCell>
                  <TableCell>
                    {release.isLatest && (
                      <Badge>最新</Badge>
                    )}
                  </TableCell>
                </TableRow>
              ))
            )}
          </TableBody>
        </Table>
      </div>
    </div>
  );
}
