"use client";

import Link from "next/link";
import { useCallback, useEffect, useMemo, useState, type ReactElement, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import {
  AlertTriangle,
  ClipboardList,
  Diamond,
  FileSearch,
  RefreshCw,
  ScrollText,
  Settings,
  Shield,
  Ticket,
  Wallet,
  BarChart3,
  Scale,
} from "lucide-react";

import { getAdminDashboard } from "@/api/admin-dashboard";
import { useAdminPlayTypeCatalog } from "@/hooks/use-admin-play-type-catalog";
import { getAdminPlayTypes } from "@/api/admin-config";
import {
  getAdminPlayTypesLoadPromise,
  getCachedAdminPlayTypes,
  resolveAdminPlayTypeDisplayName,
} from "@/lib/admin-play-types";
import {
  DashboardAnalyticsMain,
  DashboardPlayRankingCard,
} from "@/modules/dashboard/dashboard-analytics-panel";
import { DashboardCurrentDrawCard } from "@/modules/dashboard/dashboard-current-draw-card";
import { useDashboardAnalytics } from "@/modules/dashboard/use-dashboard-analytics";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Button, buttonVariants } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
import {
  AbnormalTransferPanelFooter,
  CapUsageBar,
  FinanceStructureChart,
  HotUsageBars,
  PayoutPanelSnapshot,
  ResultBatchProgress,
  DashboardPanelCard,
  SettlementStatusChart,
} from "@/modules/dashboard/dashboard-visuals";
import { useAdminCurrencyCatalog } from "@/hooks/use-admin-currency-catalog";
import { adminWeekdayKeyForDate, formatAdminCalendarToday } from "@/lib/admin-datetime";
import { normalizeAdminLanguage } from "@/i18n";
import { getAdminRequestLocale } from "@/lib/admin-locale";
import { formatAdminMinorUnits, getAdminCurrencyDecimalPlaces } from "@/lib/money";
import { cn } from "@/lib/utils";
import { LotteryApiBizError } from "@/types/api/errors";
import type { AdminDashboardDrawPanel } from "@/types/api/admin-dashboard";
import type { AdminDrawFinanceSummaryData } from "@/types/api/admin-draw-finance";
import type { AdminRiskPoolRow } from "@/types/api/admin-risk";
import type { DrawCurrentSnapshot } from "@/types/api/public-draw";

type HotPlayTab = "4D" | "3D" | "2D" | "special";

function formatMoneyMinor(minor: number, currencyCode: string | null): string {
  const code = (currencyCode ?? "NPR").toUpperCase();
  const decimals = getAdminCurrencyDecimalPlaces(code);
  const major = minor / 10 ** decimals;
  try {
    return new Intl.NumberFormat(getAdminRequestLocale(), {
      style: "currency",
      currency: code,
      minimumFractionDigits: decimals,
      maximumFractionDigits: decimals,
    }).format(major);
  } catch {
    return formatAdminMinorUnits(minor, code, decimals);
  }
}

function drawScopedHref(
  drawId: number | null,
  suffix = "",
  fallback = "/admin/draws",
): string {
  return drawId != null ? `/admin/draws/${drawId}${suffix}` : fallback;
}

function poolPlayCategory(normalizedNumber: string): HotPlayTab | "other" {
  const raw = normalizedNumber.trim();
  const digits = raw.replace(/\D/g, "");
  const digitLen = digits.length;
  const hasLetter = /[A-Za-z]/.test(raw);

  if (hasLetter && digitLen < 3) {
    return "special";
  }
  if (digitLen >= 4) {
    return "4D";
  }
  if (digitLen === 3) {
    return "3D";
  }
  if (digitLen === 2) {
    return "2D";
  }
  if (hasLetter) {
    return "special";
  }
  return "other";
}

function topPoolsForTab(pools: AdminRiskPoolRow[], tab: HotPlayTab): AdminRiskPoolRow[] {
  return [...pools]
    .filter((p) => poolPlayCategory(p.normalized_number) === tab)
    .sort((a, b) => (b.usage_ratio ?? 0) - (a.usage_ratio ?? 0))
    .slice(0, 10);
}

export function DashboardConsole(): ReactElement {
  const { t, i18n } = useTranslation(["dashboard", "common"]);
  useAdminCurrencyCatalog();
  useAdminPlayTypeCatalog();
  const todayLabel = useMemo(() => {
    const locale = normalizeAdminLanguage(i18n.resolvedLanguage ?? i18n.language);
    const weekday = t(`date.weekdays.${adminWeekdayKeyForDate()}`, { ns: "common" });

    return formatAdminCalendarToday(locale, weekday);
  }, [i18n.language, i18n.resolvedLanguage, t]);
  const [loading, setLoading] = useState(true);
  const [refreshing, setRefreshing] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const [hall, setHall] = useState<DrawCurrentSnapshot | null>(null);
  const [drawId, setDrawId] = useState<number | null>(null);
  const [drawPanel, setDrawPanel] = useState<AdminDashboardDrawPanel | null>(null);
  const [finance, setFinance] = useState<AdminDrawFinanceSummaryData | null>(null);
  const [capabilities, setCapabilities] = useState<{ draw_finance_risk: boolean; wallet_transfer_view: boolean } | null>(null);
  const [pendingReview, setPendingReview] = useState<number | null>(null);
  const [riskLocked, setRiskLocked] = useState(0);
  const [riskCap, setRiskCap] = useState(0);
  const [hotPoolSample, setHotPoolSample] = useState<AdminRiskPoolRow[]>([]);
  const [abnormalTransferTotal, setAbnormalTransferTotal] = useState<number | null>(null);
  const [hotTab, setHotTab] = useState<HotPlayTab>("4D");
  const [playOptions, setPlayOptions] = useState<{ code: string; label: string }[]>([]);

  const loadPlayOptions = useCallback(async () => {
    try {
      await getAdminPlayTypesLoadPromise(getAdminPlayTypes);
      setPlayOptions(
        getCachedAdminPlayTypes().map((item) => ({
          code: item.play_code,
          label:
            resolveAdminPlayTypeDisplayName(item.play_code, i18n.language, item) || item.play_code,
        })),
      );
    } catch {
      setPlayOptions([]);
    }
  }, [i18n.language]);

  useEffect(() => {
    queueMicrotask(() => {
      void loadPlayOptions();
    });
  }, [loadPlayOptions]);

  const load = useCallback(async (isRefresh = false) => {
    if (isRefresh) {
      setRefreshing(true);
    } else {
      setLoading(true);
    }
    setError(null);
    setFinance(null);
    setCapabilities(null);
    setDrawPanel(null);
    setPendingReview(null);
    setDrawId(null);
    setRiskLocked(0);
    setRiskCap(0);
    setHotPoolSample([]);
    setAbnormalTransferTotal(null);

    try {
      const d = await getAdminDashboard();
      setHall(d.hall);

      if (d.resolved_draw != null) {
        setDrawId(d.resolved_draw.id);
      }

      setCapabilities(d.capabilities);
      if (d.finance != null) {
        setFinance(d.finance);
      }
      if (d.draw != null) {
        setDrawPanel(d.draw);
        setPendingReview(d.draw.result_batch_counts.pending_review);
      }
      if (d.risk != null) {
        setRiskLocked(d.risk.locked_amount);
        setRiskCap(d.risk.cap_amount);
        setHotPoolSample(d.risk.hot_pool_rows);
      }
      setAbnormalTransferTotal(d.abnormal_transfer_total);
    } catch (e) {
      const msg =
        e instanceof LotteryApiBizError ? e.message : t("warnings.loadFailed");
      setError(msg);
    } finally {
      setLoading(false);
      setRefreshing(false);
    }
  }, [t]);

  useEffect(() => {
    const timer = window.setTimeout(() => {
      void load(false);
    }, 0);
    return () => window.clearTimeout(timer);
  }, [load]);

  const currency = finance?.currency_code ?? null;
  const canFinance = capabilities?.draw_finance_risk ?? false;
  const usagePct = riskCap > 0 ? (riskLocked / riskCap) * 100 : 0;

  const hotRows = useMemo(() => topPoolsForTab(hotPoolSample, hotTab), [hotPoolSample, hotTab]);

  const analytics = useDashboardAnalytics({ enabled: canFinance, playOptions });
  const showAnalytics = canFinance;

  const quickLinks: { href: string; label: string; icon: ReactNode }[] = [
    { href: "/admin/draws", label: t("quickLinks.createDrawPlan"), icon: <Diamond className="size-4" /> },
    { href: "/admin/draws", label: t("quickLinks.drawSchedule"), icon: <Ticket className="size-4" /> },
    {
      href: drawId != null ? `/admin/draws/${drawId}/results` : "/admin/draws",
      label: t("quickLinks.results"),
      icon: <FileSearch className="size-4" />,
    },
    { href: "/admin/tickets", label: t("quickLinks.tickets"), icon: <Shield className="size-4" /> },
    { href: "/admin/wallet/transactions", label: t("quickLinks.walletTransactions"), icon: <Wallet className="size-4" /> },
    { href: "/admin/audit-logs", label: t("quickLinks.auditLogs"), icon: <ScrollText className="size-4" /> },
    { href: "/admin/reports", label: t("quickLinks.reports"), icon: <BarChart3 className="size-4" /> },
    { href: "/admin/rules/odds", label: t("quickLinks.payoutRules"), icon: <Scale className="size-4" /> },
    { href: "/admin/risk", label: t("quickLinks.riskMonitor"), icon: <Shield className="size-4" /> },
    { href: "/admin/settings", label: t("quickLinks.systemSettings"), icon: <Settings className="size-4" /> },
  ];

  return (
    <div className="flex min-w-0 w-full max-w-none flex-col gap-5">
      <div className="flex flex-wrap items-center justify-between gap-2">
        <div className="min-w-0">
          <h1 className="admin-list-title">{t("title")}</h1>
          <p className="mt-0.5 text-xs text-muted-foreground">{todayLabel}</p>
        </div>
        <Button
          type="button"
          variant="outline"
          size="sm"
          className="h-8"
          disabled={loading || refreshing}
          onClick={() => void load(true)}
        >
          <RefreshCw className={cn("size-3.5", refreshing && "animate-spin")} />
          {t("actions.refresh", { ns: "common" })}
        </Button>
      </div>

      {error ? (
        <Alert className="border-amber-200 bg-amber-50 dark:border-amber-900/60 dark:bg-amber-950/30">
          <AlertTitle>{t("notice")}</AlertTitle>
          <AlertDescription>{error}</AlertDescription>
        </Alert>
      ) : null}

      {!loading && capabilities && !capabilities.draw_finance_risk ? (
        <Alert className="border-amber-200 bg-amber-50 dark:border-amber-900/60 dark:bg-amber-950/30">
          <AlertTitle>{t("notice")}</AlertTitle>
          <AlertDescription>{t("warnings.drawPermission")}</AlertDescription>
        </Alert>
      ) : null}

      <section className="flex min-w-0 flex-col gap-4">
        <DashboardCurrentDrawCard
          key={`${hall?.draw_no ?? "empty"}:${hall?.seconds_to_close ?? 0}:${loading ? "loading" : "ready"}`}
          hall={hall}
          drawId={drawId}
          loading={loading}
        />
        <div className="grid min-w-0 grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-4">
        <DashboardPanelCard
          href={drawScopedHref(drawId, "/review")}
          title={t("pendingReviewResults")}
          value={pendingReview ?? "—"}
          subtitle={t("resultBatches")}
          actionLabel={
            (pendingReview ?? 0) > 0
              ? t("actions.reviewNow", { ns: "common" })
              : t("drawDetails")
          }
          icon={<ClipboardList className="size-5" aria-hidden />}
          accent={(pendingReview ?? 0) > 0 ? "destructive" : "muted"}
          highlight={(pendingReview ?? 0) > 0}
          loading={loading}
        >
          {drawPanel ? <ResultBatchProgress draw={drawPanel} compact /> : null}
        </DashboardPanelCard>

        <DashboardPanelCard
          href="/admin/wallet/transfer-orders"
          title={t("abnormalTransferOrders")}
          value={abnormalTransferTotal ?? "—"}
          subtitle={t("abnormalTransferScope")}
          actionLabel={t("actions.viewAll", { ns: "common" })}
          icon={<AlertTriangle className="size-5" aria-hidden />}
          accent={(abnormalTransferTotal ?? 0) > 0 ? "warning" : "muted"}
          loading={loading}
          highlight={(abnormalTransferTotal ?? 0) > 0}
        >
          <AbnormalTransferPanelFooter
            total={abnormalTransferTotal}
            walletPermission={capabilities?.wallet_transfer_view ?? true}
          />
        </DashboardPanelCard>

        <DashboardPanelCard
          href={drawScopedHref(drawId, "/risk/occupancy", "/admin/risk")}
          title={t("riskCapUsage")}
          value={`${usagePct.toFixed(1)}%`}
          subtitle={t("lockedAndCap", {
            locked: formatMoneyMinor(riskLocked, currency),
            cap: formatMoneyMinor(riskCap, currency),
          })}
          actionLabel={t("occupancyDetails")}
          icon={<Shield className="size-5" aria-hidden />}
          accent={
            usagePct >= 90 ? "destructive" : usagePct >= 70 ? "primary" : "muted"
          }
          loading={loading}
        >
          <CapUsageBar
            locked={riskLocked}
            cap={riskCap}
            usagePct={usagePct}
            formatMoney={formatMoneyMinor}
            currency={currency}
            compact
          />
        </DashboardPanelCard>

        <DashboardPanelCard
          href={drawScopedHref(drawId, "/finance")}
          title={t("payoutComposition")}
          value={
            finance
              ? formatMoneyMinor(finance.total_payout_minor, currency)
              : "—"
          }
          subtitle={
            finance
              ? t("orderAndTicket", {
                  orders: finance.order_count,
                  tickets: finance.ticket_item_count,
                })
              : t("states.noData", { ns: "common" })
          }
          actionLabel={t("detailsShort")}
          icon={<Wallet className="size-5" aria-hidden />}
          accent="primary"
          loading={loading}
        >
          {finance ? (
            <PayoutPanelSnapshot finance={finance} formatMoney={formatMoneyMinor} />
          ) : null}
        </DashboardPanelCard>
        </div>
      </section>

      <section
        className={cn(
          "grid min-w-0 grid-cols-1 gap-4",
          showAnalytics ? "xl:grid-cols-12" : "xl:grid-cols-1",
        )}
      >
        <div
          className={cn(
            "flex min-w-0 flex-col gap-4",
            showAnalytics ? "xl:col-span-8" : "xl:col-span-12",
          )}
        >
          {showAnalytics ? <DashboardAnalyticsMain analytics={analytics} /> : null}

          <div className="grid min-w-0 grid-cols-1 gap-4 md:grid-cols-2">
            <Card className="admin-list-card py-0">
              <CardHeader className="flex flex-row items-center justify-between space-y-0 border-b border-border/60 px-4 py-3">
                <CardTitle className="text-sm font-semibold">{t("settlementOverview")}</CardTitle>
                {drawId != null ? (
                  <Link
                    href="/admin/settlement-batches"
                    className={cn(buttonVariants({ variant: "outline", size: "sm" }), "h-7 px-2 text-xs")}
                  >
                    {t("actions.viewAll", { ns: "common" })}
                  </Link>
                ) : null}
              </CardHeader>
              <CardContent className="px-4 py-4">
                {loading ? (
                  <Skeleton className="h-40 w-full" />
                ) : finance ? (
                  <SettlementStatusChart finance={finance} />
                ) : (
                  <p className="py-10 text-center text-xs text-muted-foreground">
                    {t("states.noData", { ns: "common" })}
                  </p>
                )}
              </CardContent>
            </Card>

            <Card className="admin-list-card py-0">
              <CardHeader className="flex flex-row flex-wrap items-center justify-between gap-2 space-y-0 border-b border-border/60 px-4 py-3">
                <CardTitle className="text-sm font-semibold">{t("hotNumbersTop10")}</CardTitle>
                <div className="flex flex-wrap items-center gap-1">
                  <div role="tablist" aria-label={t("playDimension")} className="flex gap-0.5">
                    {([
                      { value: "4D", label: t("tabs.4d") },
                      { value: "3D", label: t("tabs.3d") },
                      { value: "2D", label: t("tabs.2d") },
                      { value: "special", label: t("tabs.special") },
                    ] as const).map((tab) => (
                      <button
                        key={tab.value}
                        type="button"
                        role="tab"
                        aria-selected={hotTab === tab.value}
                        className={cn(
                          "rounded px-2 py-0.5 text-[11px] font-medium transition-colors",
                          hotTab === tab.value
                            ? "bg-primary text-primary-foreground"
                            : "text-muted-foreground hover:bg-muted",
                        )}
                        onClick={() => setHotTab(tab.value)}
                      >
                        {tab.label}
                      </button>
                    ))}
                  </div>
                </div>
              </CardHeader>
              <CardContent className="px-4 py-4">
                {loading ? (
                  <Skeleton className="h-48 w-full" />
                ) : (
                  <HotUsageBars rows={hotRows} compact />
                )}
              </CardContent>
            </Card>
          </div>

          {!showAnalytics ? (
            <div className="grid min-w-0 grid-cols-1 gap-4 md:grid-cols-2">
              <Card className="admin-list-card min-w-0 py-0">
                <CardHeader className="border-b border-border/60 px-4 py-3 pb-0">
                  <CardTitle className="text-sm font-semibold">{t("financeStructure")}</CardTitle>
                </CardHeader>
                <CardContent className="px-4 py-4">
                  {loading ? (
                    <Skeleton className="h-52 w-full" />
                  ) : finance ? (
                    <FinanceStructureChart finance={finance} formatMoney={formatMoneyMinor} />
                  ) : (
                    <p className="py-10 text-center text-xs text-muted-foreground">
                      {t("states.noData", { ns: "common" })}
                    </p>
                  )}
                </CardContent>
              </Card>

              <Card className="admin-list-card min-w-0 py-0">
                <CardHeader className="border-b border-border/60 px-4 py-3 pb-0">
                  <CardTitle className="text-sm font-semibold">{t("quickLinksTitle")}</CardTitle>
                </CardHeader>
                <CardContent className="grid grid-cols-2 gap-2 px-4 py-4 sm:grid-cols-3">
                  {quickLinks.map((q) => (
                    <Link
                      key={q.href + q.label}
                      href={q.href}
                      className="flex min-w-0 flex-col items-center gap-2 rounded-lg border border-border/70 bg-muted/10 px-2 py-3 text-center text-[11px] font-medium leading-snug text-foreground transition-colors hover:border-primary/30 hover:bg-muted/30"
                    >
                      <span className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-card text-primary shadow-sm">
                        {q.icon}
                      </span>
                      <span className="line-clamp-2">{q.label}</span>
                    </Link>
                  ))}
                </CardContent>
              </Card>
            </div>
          ) : null}
        </div>

        {showAnalytics ? (
          <aside className="flex min-w-0 flex-col gap-4 xl:col-span-4">
            <DashboardPlayRankingCard analytics={analytics} />

            <Card className="admin-list-card min-w-0 py-0">
            <CardHeader className="border-b border-border/60 px-4 py-3 pb-0">
              <CardTitle className="text-sm font-semibold">{t("financeStructure")}</CardTitle>
            </CardHeader>
            <CardContent className="px-4 py-4">
              {loading ? (
                <Skeleton className="h-52 w-full" />
              ) : finance ? (
                <FinanceStructureChart finance={finance} formatMoney={formatMoneyMinor} />
              ) : (
                <p className="py-10 text-center text-xs text-muted-foreground">
                  {t("states.noData", { ns: "common" })}
                </p>
              )}
            </CardContent>
          </Card>

          <Card className="admin-list-card py-0">
            <CardHeader className="border-b border-border/60 px-4 py-3 pb-0">
              <CardTitle className="text-sm font-semibold">{t("quickLinksTitle")}</CardTitle>
            </CardHeader>
            <CardContent className="grid grid-cols-2 gap-2 px-4 py-4 sm:grid-cols-3">
              {quickLinks.map((q) => (
                <Link
                  key={q.href + q.label}
                  href={q.href}
                  className="flex min-w-0 flex-col items-center gap-2 rounded-lg border border-border/70 bg-muted/10 px-2 py-3 text-center text-[11px] font-medium leading-snug text-foreground transition-colors hover:border-primary/30 hover:bg-muted/30"
                >
                  <span className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-card text-primary shadow-sm">
                    {q.icon}
                  </span>
                  <span className="line-clamp-2">{q.label}</span>
                </Link>
              ))}
            </CardContent>
          </Card>
          </aside>
        ) : null}
      </section>
    </div>
  );
}
