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 AdminCouponsPage() {
  const coupons = await prisma.coupon.findMany({
    orderBy: { id: "desc" },
  });

  return (
    <div>
      <div className="flex justify-between items-center mb-6">
        <h1 className="text-2xl font-bold">کوپن‌ها</h1>
        <Button asChild>
          <Link href="/admin/coupons/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>
                <th className="text-right p-4">فعال</th>
                <th className="text-right p-4">عملیات</th>
              </tr>
            </thead>
            <tbody>
              {coupons.map((c) => (
                <tr key={c.id} className="border-b last:border-0">
                  <td className="p-4 font-mono">{c.code}</td>
                  <td className="p-4">{c.type}</td>
                  <td className="p-4">{Number(c.value).toLocaleString("fa-IR")}</td>
                  <td className="p-4">{new Date(c.expiresAt).toLocaleDateString("fa-IR")}</td>
                  <td className="p-4">
                    {c.redeemedCount} / {c.maxRedemptions}
                  </td>
                  <td className="p-4">{c.active ? "بله" : "خیر"}</td>
                  <td className="p-4">
                    <Link href={`/admin/coupons/${c.id}`} className="underline">
                      ویرایش
                    </Link>
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </CardContent>
      </Card>
    </div>
  );
}
