import Link from "next/link";
import { prisma } from "@/lib/prisma";
import { Card, CardContent } from "@/components/ui/card";
import { Button } from "@/components/ui/button";

export default async function AdminBlogPage() {
  const posts = await prisma.blogPost.findMany({
    orderBy: { createdAt: "desc" },
    include: { category: { select: { nameFa: true } } },
  });

  return (
    <div>
      <div className="flex justify-between items-center mb-6">
        <h1 className="text-2xl font-bold">وبلاگ</h1>
        <Button asChild>
          <Link href="/admin/blog/new">پست جدید</Link>
        </Button>
      </div>
      <Card>
        <CardContent className="p-0">
          <table className="w-full text-sm">
            <thead>
              <tr className="border-b">
                <th className="text-right p-4">عنوان</th>
                <th className="text-right p-4">دسته</th>
                <th className="text-right p-4">وضعیت</th>
                <th className="text-right p-4">تاریخ</th>
                <th className="text-right p-4">عملیات</th>
              </tr>
            </thead>
            <tbody>
              {posts.map((p) => (
                <tr key={p.id} className="border-b last:border-0">
                  <td className="p-4">{p.titleFa}</td>
                  <td className="p-4">{p.category.nameFa}</td>
                  <td className="p-4">{p.status}</td>
                  <td className="p-4">
                    {p.publishedAt
                      ? new Date(p.publishedAt).toLocaleDateString("fa-IR")
                      : "-"}
                  </td>
                  <td className="p-4">
                    <Link href={`/admin/blog/${p.id}`} className="underline">
                      ویرایش
                    </Link>
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </CardContent>
      </Card>
    </div>
  );
}
