import { notFound } from "next/navigation";
import type { FortuneType } from "@prisma/client";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { FortuneForm } from "../../components/fortune-form";
import { getPersianDateParts } from "@/lib/persian-date";

function parseFortuneType(value: string | undefined): FortuneType | null {
  if (value === "daily") return "DAILY";
  if (value === "monthly") return "MONTHLY";
  return null;
}

export default async function AdminFortuneNewPage({
  searchParams,
}: {
  searchParams: Promise<{ type?: string }>;
}) {
  const { type: typeParam } = await searchParams;
  const type = parseFortuneType(typeParam);
  if (!type) notFound();

  const persian = getPersianDateParts(new Date());

  return (
    <div>
      <h1 className="text-2xl font-bold mb-6">
        {type === "DAILY" ? "فال روزانه جدید" : "فال ماهانه جدید"}
      </h1>
      <Card>
        <CardHeader>
          <CardTitle className="text-base">اطلاعات فال</CardTitle>
        </CardHeader>
        <CardContent>
          <FortuneForm
            fortuneType={type}
            initial={{
              type,
              titleFa: "",
              slug: "",
              excerptFa: "",
              content: "",
              coverImageUrl: null,
              status: "DRAFT",
              persianYear: persian?.year ?? 1404,
              persianMonth: persian?.month ?? 1,
              persianDay: persian?.day ?? 1,
            }}
          />
        </CardContent>
      </Card>
    </div>
  );
}
