import Link from "next/link";
import { prisma } from "@/lib/prisma";
import { Card, CardContent } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { formatFortunePeriod } from "@/lib/fortunes";
import { getHomeFortunesSettings } from "@/lib/home-fortunes";
import { HomeFortunesEditor } from "./home-fortunes-editor";

export default async function AdminFortunesPage() {
  const [fortunes, homeFortunes] = await Promise.all([
    prisma.periodFortune.findMany({
      orderBy: [{ type: "asc" }, { createdAt: "desc" }],
    }),
    getHomeFortunesSettings(),
  ]);

  const dailyFortunes = fortunes.filter((f) => f.type === "DAILY");
  const monthlyFortunes = fortunes.filter((f) => f.type === "MONTHLY");

  function FortuneTable({ items, typeLabel }: { items: typeof fortunes; typeLabel: string }) {
    return (
      <Card className="mb-8">
        <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">{typeLabel}</th>
                <th className="text-right p-4">وضعیت</th>
                <th className="text-right p-4">عملیات</th>
              </tr>
            </thead>
            <tbody>
              {items.length === 0 ? (
                <tr>
                  <td colSpan={4} className="p-4 text-muted-foreground text-center">
                    هنوز موردی ثبت نشده است.
                  </td>
                </tr>
              ) : (
                items.map((f) => (
                  <tr key={f.id} className="border-b last:border-0">
                    <td className="p-4">{f.titleFa}</td>
                    <td className="p-4">{formatFortunePeriod(f)}</td>
                    <td className="p-4">{f.status === "PUBLISHED" ? "منتشر شده" : "پیش‌نویس"}</td>
                    <td className="p-4">
                      <Link href={`/admin/fortunes/${f.id}`} className="underline">
                        ویرایش
                      </Link>
                    </td>
                  </tr>
                ))
              )}
            </tbody>
          </table>
        </CardContent>
      </Card>
    );
  }

  return (
    <div>
      <div className="flex flex-wrap justify-between items-center gap-4 mb-6">
        <h1 className="text-2xl font-bold">فال روزانه و ماهانه</h1>
        <div className="flex gap-2">
          <Button asChild variant="outline">
            <Link href="/admin/fortunes/new?type=daily">فال روزانه جدید</Link>
          </Button>
          <Button asChild>
            <Link href="/admin/fortunes/new?type=monthly">فال ماهانه جدید</Link>
          </Button>
        </div>
      </div>

      <HomeFortunesEditor initial={homeFortunes} />

      <h2 className="text-lg font-semibold mb-3">فال روزانه</h2>
      <FortuneTable items={dailyFortunes} typeLabel="تاریخ" />

      <h2 className="text-lg font-semibold mb-3">فال ماهانه</h2>
      <FortuneTable items={monthlyFortunes} typeLabel="ماه شمسی" />
    </div>
  );
}
