import { redirect } from "next/navigation";
import Link from "next/link";
import { getSession } from "@/lib/auth/session";
import { getAdminBadgeCounts } from "@/lib/admin/badge-counts";
import { SignOutButton } from "@/components/sign-out-button";
import { SiteFooter } from "@/components/site-footer";

type NavBadgeKey = keyof Awaited<ReturnType<typeof getAdminBadgeCounts>>;

export default async function AdminLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const session = await getSession();
  if (!session?.user || (session.user as { role?: string }).role !== "ADMIN") {
    redirect("/");
  }

  const badges = await getAdminBadgeCounts();

  const nav: Array<{ href: string; label: string; badgeKey?: NavBadgeKey }> = [
    { href: "/admin", label: "داشبورد" },
    { href: "/admin/slider", label: "اسلایدر" },
    { href: "/admin/home-energy", label: "انرژی صفحه اصلی" },
    { href: "/admin/reading-spreads", label: "نمایش انواع فال" },
    { href: "/admin/theme", label: "ظاهر سایت" },
    { href: "/admin/users", label: "کاربران" },
    { href: "/admin/readings", label: "فال‌ها" },
    { href: "/admin/readings/queue", label: "صف پاسخ فال", badgeKey: "readingQueue" },
    { href: "/admin/reading-pricing", label: "قیمت فال‌ها" },
    { href: "/admin/cards", label: "کارت‌ها" },
    { href: "/admin/blog", label: "وبلاگ" },
    { href: "/admin/fortunes", label: "فال روزانه و ماهانه" },
    { href: "/admin/comments", label: "نظرات", badgeKey: "pendingComments" },
    { href: "/admin/coupons", label: "کوپن‌ها" },
    { href: "/admin/pages", label: "صفحات سایت" },
  ];

  return (
    <div className="min-h-screen flex flex-col">
      <div className="flex flex-1">
        <aside className="w-56 border-l flex flex-col p-4">
          <Link href="/admin" className="font-bold mb-6">
            پنل مدیریت
          </Link>
          <nav className="flex flex-col gap-2 flex-1">
            {nav.map((item) => {
              const badgeCount = item.badgeKey ? badges[item.badgeKey] : 0;
              return (
                <Link
                  key={item.href}
                  href={item.href}
                  className="flex items-center justify-between gap-2 text-sm hover:underline py-1"
                >
                  <span>{item.label}</span>
                  {badgeCount > 0 ? (
                    <span
                      className="min-w-5 shrink-0 rounded-full bg-primary px-1.5 py-0.5 text-center text-[10px] font-semibold leading-none text-primary-foreground"
                      aria-label={`${badgeCount.toLocaleString("fa-IR")} مورد در انتظار`}
                    >
                      {badgeCount.toLocaleString("fa-IR")}
                    </span>
                  ) : null}
                </Link>
              );
            })}
          </nav>
          <div className="pt-4 border-t mt-auto">
            <p className="text-xs text-muted-foreground mb-2 truncate" title={session.user.phone ?? ""}>
              {session.user.phone}
            </p>
            <SignOutButton />
          </div>
        </aside>
        <main className="flex-1 p-8 overflow-auto">{children}</main>
      </div>
      <SiteFooter />
    </div>
  );
}
