"use client";

import { Trash2 } from "lucide-react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";

import {
  deleteRiskCapVersion,
  getAllConfigVersions,
  getRiskCapVersion,
  getRiskCapVersions,
  postRiskCapVersion,
  publishRiskCapVersion,
  putRiskCapItems,
} from "@/api/admin-config";
import { getAdminDraws } from "@/api/admin-draws";
import { AdminPageCard } from "@/components/admin/admin-page-card";
import { AdminRowActionsMenu } from "@/components/admin/admin-row-actions-menu";
import { Button } from "@/components/ui/button";
import { ConfigDocPage, ConfigDocToolbar } from "@/modules/config/config-doc-page";
import { ConfigVersionToolbarMeta } from "@/modules/config/config-version-toolbar-meta";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { AdminLoadingState } from "@/components/admin/admin-loading-state";
import { ConfigVersionSwitcher } from "@/modules/config/config-version-switcher";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";

import { ConfigReadonlyValue } from "@/modules/config/config-readonly-value";
import { ConfigVersionActions } from "@/modules/config/config-version-actions";
import { useAdminDateTimeFormatter } from "@/hooks/use-admin-datetime-formatter";
import { useAsyncEffect } from "@/hooks/use-async-effect";
import { useConfirmAction } from "@/hooks/use-confirm-action";
import { useTranslationRef } from "@/hooks/use-translation-ref";
import { adminHasAnyPermission } from "@/lib/admin-permissions";
import { formatAdminMinorDecimal, parseAdminMajorToMinor } from "@/lib/money";
import { PRD_RISK_CAP_MANAGE } from "@/lib/admin-prd";
import { useAdminProfile } from "@/stores/admin-session";
import { LotteryApiBizError } from "@/types/api/errors";
import { pickDefaultConfigVersionId } from "@/lib/config-version-auto-pick";
import type { AdminDrawListItem } from "@/types/api/admin-draws";
import type {
  ConfigVersionSummary,
  RiskCapItemRow,
  RiskCapVersionDetail,
} from "@/types/api/admin-config";

type DraftRiskRow = Omit<RiskCapItemRow, "id"> & { clientKey: string };

function newRow(): DraftRiskRow {
  return {
    clientKey: `new-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
    draw_id: null,
    normalized_number: "0000",
    cap_amount: 0,
    cap_type: "per_number",
  };
}

function isDefaultRiskRow(row: DraftRiskRow): boolean {
  return row.cap_type === "default";
}

function defaultRiskRowFromAmount(amount: number): DraftRiskRow {
  return {
    clientKey: `default-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
    draw_id: null,
    normalized_number: "0000",
    cap_amount: amount,
    cap_type: "default",
  };
}

function formatMinorToEditableMajor(minor: number, currencyCode: string): string {
  return formatAdminMinorDecimal(minor, currencyCode).replace(/,/g, "");
}

function syncDefaultCapInput(rows: DraftRiskRow[], currencyCode: string): string {
  const defaultRow = rows.find(isDefaultRiskRow);
  if (!defaultRow) {
    return "";
  }
  return formatMinorToEditableMajor(defaultRow.cap_amount, currencyCode);
}

export function RiskCapDocScreen() {
  const { t } = useTranslation(["config", "adminUsers", "common"]);
  const tRef = useTranslationRef(["config", "common"]);
  const { request: requestConfirm, ConfirmDialog } = useConfirmAction();
  const profile = useAdminProfile();
  const canManage = adminHasAnyPermission(profile?.permissions, [PRD_RISK_CAP_MANAGE]);
  const formatDt = useAdminDateTimeFormatter();
  const [list, setList] = useState<ConfigVersionSummary[]>([]);
  const [selectedId, setSelectedId] = useState("");
  const [detail, setDetail] = useState<RiskCapVersionDetail | null>(null);
  const [draftRows, setDraftRows] = useState<DraftRiskRow[]>([]);
  const [defaultCapInput, setDefaultCapInput] = useState("");
  const [loadingList, setLoadingList] = useState(true);
  const [loadingDetail, setLoadingDetail] = useState(false);
  const [saving, setSaving] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [drawOptions, setDrawOptions] = useState<AdminDrawListItem[]>([]);
  const [rollbackOpen, setRollbackOpen] = useState(false);
  const [rollbackTarget, setRollbackTarget] = useState<ConfigVersionSummary | null>(null);
  const [showNumberCaps, setShowNumberCaps] = useState(false);

  const amountCurrencyCode = "NPR";

  const refreshList = useCallback(async () => {
    setLoadingList(true);
    setError(null);
    try {
      const d = await getAllConfigVersions(getRiskCapVersions);
      setList(d.items);
    } catch (e) {
      const msg =
        e instanceof LotteryApiBizError ? e.message : tRef.current("errors.loadFailed", { ns: "common" });
      setError(msg);
      setList([]);
    } finally {
      setLoadingList(false);
    }
  }, [tRef]);

  useAsyncEffect(() => {
    void refreshList();
  }, [tRef]);

  const loadDrawOptions = useCallback(async () => {
    try {
      const data = await getAdminDraws({ page: 1, per_page: 100 });
      setDrawOptions(data.items);
    } catch {
      setDrawOptions([]);
    }
  }, []);

  useAsyncEffect(() => {
    void loadDrawOptions();
  }, []);

  const applyDefaultCapInput = useCallback(
    (value: string) =>
      (prev: DraftRiskRow[]): DraftRiskRow[] => {
        const n = parseAdminMajorToMinor(value, amountCurrencyCode);
        const rest = prev.filter((row) => !isDefaultRiskRow(row));
        if (n != null && n > 0) {
          return [defaultRiskRowFromAmount(n), ...rest];
        }
        return rest;
      },
    [amountCurrencyCode],
  );

  const loadDetail = useCallback(
    async (id: number) => {
      setLoadingDetail(true);
      try {
        const d = await getRiskCapVersion(id);
        setDetail(d);
        const mapped = d.items.map((it) => ({
          clientKey: `srv-${it.id}`,
          draw_id: it.draw_id,
          normalized_number: it.normalized_number,
          cap_amount: it.cap_amount,
          cap_type: it.cap_type,
        }));
        setDraftRows(mapped);
        setDefaultCapInput(syncDefaultCapInput(mapped, amountCurrencyCode));
        setShowNumberCaps(mapped.some((row) => !isDefaultRiskRow(row)));
      } catch (e) {
        toast.error(
          e instanceof LotteryApiBizError ? e.message : tRef.current("errors.loadFailed", { ns: "common" }),
        );
        setDetail(null);
        setDraftRows([]);
        setDefaultCapInput("");
        setShowNumberCaps(false);
      } finally {
        setLoadingDetail(false);
      }
    },
    [amountCurrencyCode, tRef],
  );

  useEffect(() => {
    if (list.length === 0) {
      if (selectedId !== "") {
        queueMicrotask(() => {
          setSelectedId("");
          setDetail(null);
          setDraftRows([]);
          setDefaultCapInput("");
        });
      }
      return;
    }
    if (selectedId !== "" && list.some((x) => String(x.id) === selectedId)) {
      return;
    }
    queueMicrotask(() => {
      const pickId = pickDefaultConfigVersionId(list);
      if (pickId) {
        setSelectedId(pickId);
      }
    });
  }, [list, selectedId]);

  useEffect(() => {
    if (selectedId === "") {
      return;
    }
    const id = Number(selectedId);
    if (!Number.isFinite(id)) {
      return;
    }
    queueMicrotask(() => {
      void loadDetail(id);
    });
  }, [selectedId, loadDetail]);

  const selectedVersionSummary = useMemo(
    () => list.find((x) => String(x.id) === selectedId) ?? null,
    [list, selectedId],
  );
  const isSelectedDetail = detail !== null && String(detail.id) === selectedId;
  const selectedStatus = isSelectedDetail ? detail.status : selectedVersionSummary?.status;
  const isDraft = selectedStatus === "draft";
  const canEditDraft = isDraft && canManage;

  const updateRow = (idx: number, patch: Partial<DraftRiskRow>) => {
    setDraftRows((prev) => prev.map((r, i) => (i === idx ? { ...r, ...patch } : r)));
  };

  function removeRow(idx: number) {
    setDraftRows((prev) => prev.filter((_, i) => i !== idx));
  }

  async function handleSave() {
    if (!detail || !canEditDraft) {
      return;
    }
    const rowsToSave = applyDefaultCapInput(defaultCapInput)(draftRows);
    setDraftRows(rowsToSave);
    if (rowsToSave.length === 0) {
      toast.error(t("riskCap.validation.requireAtLeastOne", { ns: "config" }));
      return;
    }
    for (const r of rowsToSave) {
      if (isDefaultRiskRow(r)) {
        if (r.cap_amount <= 0) {
          toast.error(t("riskCap.validation.defaultGreaterThanZero", { ns: "config" }));
          return;
        }
        if (r.draw_id !== null) {
          toast.error(t("riskCap.validation.defaultCannotBindDraw", { ns: "config" }));
          return;
        }
        continue;
      }
      if (!/^[0-9]{4}$/.test(r.normalized_number)) {
        toast.error(t("riskCap.validation.numberMustBe4Digits", { ns: "config", number: r.normalized_number }));
        return;
      }
      if (r.cap_amount <= 0) {
        toast.error(t("riskCap.validation.specialGreaterThanZero", { ns: "config", number: r.normalized_number }));
        return;
      }
    }
    setSaving(true);
    try {
      const payload = rowsToSave.map((r) => ({
        draw_id: r.draw_id && r.draw_id > 0 ? r.draw_id : null,
        normalized_number: r.normalized_number,
        cap_amount: r.cap_amount,
        cap_type: r.cap_type,
      }));
      const d = await putRiskCapItems(detail.id, payload);
      setDetail(d);
      const saved = d.items.map((it) => ({
        clientKey: `srv-${it.id}`,
        draw_id: it.draw_id,
        normalized_number: it.normalized_number,
        cap_amount: it.cap_amount,
        cap_type: it.cap_type,
      }));
      setDraftRows(saved);
      setDefaultCapInput(syncDefaultCapInput(saved, amountCurrencyCode));
      toast.success(t("versionActions.saveDraft", { ns: "config" }));
      void refreshList();
    } catch (e) {
      toast.error(e instanceof LotteryApiBizError ? e.message : t("versionActions.saveFailed", { ns: "config" }));
    } finally {
      setSaving(false);
    }
  }

  async function handlePublish() {
    if (!detail || !canEditDraft) {
      return;
    }
    setSaving(true);
    try {
      const d = await publishRiskCapVersion(detail.id);
      setDetail(d);
      const pub = d.items.map((it) => ({
        clientKey: `srv-${it.id}`,
        draw_id: it.draw_id,
        normalized_number: it.normalized_number,
        cap_amount: it.cap_amount,
        cap_type: it.cap_type,
      }));
      setDraftRows(pub);
      setDefaultCapInput(syncDefaultCapInput(pub, amountCurrencyCode));
      toast.success(t("versionActions.publishCurrent", { ns: "config" }));
      void refreshList();
      setSelectedId(String(d.id));
    } catch (e) {
      toast.error(e instanceof LotteryApiBizError ? e.message : t("riskCap.publishFailed", { ns: "config" }));
    } finally {
      setSaving(false);
    }
  }

  async function handleNewDraft() {
    setSaving(true);
    try {
      const active = list.find((x) => x.status === "active");
      const d = await postRiskCapVersion({
        reason: `draft ${new Date().toISOString()}`,
        clone_from_version_id: active?.id ?? null,
      });
      toast.success(t("riskCap.createDraftSuccess", { ns: "config", version: d.version_no }));
      await refreshList();
      setSelectedId(String(d.id));
      setDetail(d);
      const nd = d.items.map((it) => ({
        clientKey: `srv-${it.id}`,
        draw_id: it.draw_id,
        normalized_number: it.normalized_number,
        cap_amount: it.cap_amount,
        cap_type: it.cap_type,
      }));
      setDraftRows(nd);
      setDefaultCapInput(syncDefaultCapInput(nd, amountCurrencyCode));
    } catch (e) {
      toast.error(e instanceof LotteryApiBizError ? e.message : t("riskCap.createDraftFailed", { ns: "config" }));
    } finally {
      setSaving(false);
    }
  }

  const specialRows = useMemo(
    () =>
      draftRows
        .map((row, index) => ({ row, index }))
        .filter(({ row }) => !isDefaultRiskRow(row) && row.draw_id == null),
    [draftRows],
  );

  const drawBoundRows = useMemo(
    () => draftRows.filter((row) => !isDefaultRiskRow(row) && row.draw_id != null),
    [draftRows],
  );

  function drawLabel(drawId: number): string {
    return drawOptions.find((draw) => draw.id === drawId)?.draw_no ?? String(drawId);
  }

  const defaultCapDisplay = detail
    ? formatAdminMinorDecimal(draftRows.find(isDefaultRiskRow)?.cap_amount ?? 0, amountCurrencyCode)
    : formatAdminMinorDecimal(0, amountCurrencyCode);

  async function handleDeleteVersion(row: ConfigVersionSummary) {
    try {
      await deleteRiskCapVersion(row.id);
      toast.success(t("versionSwitcher.delete", { ns: "config" }));
      await refreshList();
    } catch (e) {
      toast.error(e instanceof LotteryApiBizError ? e.message : t("riskCap.deleteFailed", { ns: "config" }));
      throw e;
    }
  }

  function requestRollback(row: ConfigVersionSummary) {
    setRollbackTarget(row);
    setRollbackOpen(true);
  }

  async function handleRollback() {
    if (!rollbackTarget) {
      return;
    }
    setSaving(true);
    try {
      const d = await postRiskCapVersion({
        reason: `rollback from v${rollbackTarget.version_no}`,
        clone_from_version_id: rollbackTarget.id,
      });
      toast.success(
        t("versionActions.rollbackSuccess", {
          ns: "config",
          fromVersion: rollbackTarget.version_no,
          version: d.version_no,
        }),
      );
      await refreshList();
      setSelectedId(String(d.id));
      setDetail(d);
      const mapped = d.items.map((it) => ({
        clientKey: `srv-${it.id}`,
        draw_id: it.draw_id,
        normalized_number: it.normalized_number,
        cap_amount: it.cap_amount,
        cap_type: it.cap_type,
      }));
      setDraftRows(mapped);
      setDefaultCapInput(syncDefaultCapInput(mapped, amountCurrencyCode));
      setRollbackOpen(false);
      setRollbackTarget(null);
    } catch (e) {
      toast.error(e instanceof LotteryApiBizError ? e.message : t("versionActions.rollbackFailed", { ns: "config" }));
    } finally {
      setSaving(false);
    }
  }

  return (
    <ConfigDocPage
      title={t("nav.items.risk-cap", { ns: "config" })}
      titleSuffix={detail ? `· v${detail.version_no}` : undefined}
      toolbar={
        <ConfigDocToolbar
          switcher={
            <ConfigVersionSwitcher
              versions={list}
              selectedId={selectedId}
              onSelectedIdChange={setSelectedId}
              loading={loadingList}

              onDeleteVersion={handleDeleteVersion}
              onRollbackVersion={requestRollback}
              rollbackBusy={saving}
            />
          }
          actions={
            <ConfigVersionActions
              isDraft={isDraft}
              canManage={canManage}
              loadingList={loadingList}
              loadingDetail={loadingDetail}
              saving={saving}
              onRefresh={() => void refreshList()}
              onNewDraft={() => void handleNewDraft()}
              onSaveDraft={() => void handleSave()}
              onPublish={() =>
                requestConfirm({
                  title: t("riskCap.publishDialog.title", { ns: "config" }),
                  description: t("riskCap.publishDialog.description", { ns: "config" }),
                  confirmLabel: t("riskCap.publishDialog.confirm", { ns: "config" }),
                  confirmVariant: "destructive",
                  onConfirm: () => handlePublish(),
                })
              }
            />
          }
          footer={
            detail?.effective_at ? (
              <ConfigVersionToolbarMeta>
                <span>
                  {t("riskCap.effectiveAt", {
                    ns: "config",
                    value: formatDt(detail.effective_at),
                  })}
                </span>
              </ConfigVersionToolbarMeta>
            ) : null
          }
        />
      }
      contentClassName="space-y-6"
    >
      {error ? <p className="text-sm text-destructive">{error}</p> : null}

      <AdminPageCard title={t("riskCap.defaultCap.title", { ns: "config" })}>
        <div className="max-w-xs space-y-2">
          <Label htmlFor="default-cap">{t("riskCap.defaultCap.fieldLabel", { ns: "config" })}</Label>
          {canEditDraft ? (
            <Input
              id="default-cap"
              type="text"
              inputMode="decimal"
              className="font-semibold tabular-nums"
              disabled={saving}
              value={defaultCapInput}
              placeholder={t("riskCap.placeholders.defaultCap", { ns: "config" })}
              onChange={(e) => setDefaultCapInput(e.target.value)}
              onBlur={() => setDraftRows(applyDefaultCapInput(defaultCapInput))}
            />
          ) : (
            <ConfigReadonlyValue className="font-semibold tabular-nums">{defaultCapDisplay}</ConfigReadonlyValue>
          )}
        </div>
      </AdminPageCard>

      {canEditDraft && !showNumberCaps && specialRows.length === 0 && drawBoundRows.length === 0 ? (
        <Button
          type="button"
          variant="outline"
          size="sm"
          disabled={saving || loadingDetail}
          onClick={() => {
            setShowNumberCaps(true);
            setDraftRows((prev) => [...prev, newRow()]);
          }}
        >
          {t("riskCap.actions.addNumberCap", { ns: "config" })}
        </Button>
      ) : null}

      {showNumberCaps || specialRows.length > 0 || drawBoundRows.length > 0 ? (
        <AdminPageCard
          title={t("riskCap.specialCaps.title", { ns: "config" })}
          actions={
            canEditDraft ? (
              <Button
                type="button"
                variant="outline"
                size="sm"
                disabled={saving}
                onClick={() => setDraftRows((prev) => [...prev, newRow()])}
              >
                {t("riskCap.actions.addSpecialCap", { ns: "config" })}
              </Button>
            ) : null
          }
        >
          {loadingDetail ? (
            <AdminLoadingState
              minHeight="6rem"
              className="py-4"
              label={t("riskCap.loadingDetails", { ns: "config" })}
            />
          ) : specialRows.length === 0 && drawBoundRows.length === 0 ? (
            <p className="text-sm text-muted-foreground">{t("riskCap.noDetailRows", { ns: "config" })}</p>
          ) : (
            <div className="space-y-4">
              {specialRows.length > 0 ? (
                <div className="admin-table-shell">
                  <Table>
                    <TableHeader>
                      <TableRow>
                        <TableHead className="w-[110px]">{t("riskCap.table.number", { ns: "config" })}</TableHead>
                        <TableHead className="w-[140px]">{t("riskCap.table.capAmount", { ns: "config" })}</TableHead>
                        <TableHead className="sticky right-0 z-20 w-14 bg-muted text-center shadow-[-1px_0_0_rgba(203,213,225,0.7)]">
                          {t("riskCap.table.actions", { ns: "config" })}
                        </TableHead>
                      </TableRow>
                    </TableHeader>
                    <TableBody>
                      {specialRows.map(({ row: r, index: idx }) => (
                        <TableRow key={r.clientKey}>
                          <TableCell>
                      {canEditDraft ? (
                        <Input
                          className="h-8 font-mono tabular-nums"
                          maxLength={4}
                          disabled={saving}
                          value={r.normalized_number}
                          placeholder={t("riskCap.placeholders.number", { ns: "config" })}
                          onChange={(e) =>
                            updateRow(idx, {
                              normalized_number: e.target.value.replace(/\D/g, "").slice(0, 4),
                            })
                          }
                        />
                      ) : (
                        <ConfigReadonlyValue mono>{r.normalized_number}</ConfigReadonlyValue>
                      )}
                    </TableCell>
                    <TableCell>
                      {canEditDraft ? (
                        <Input
                          type="text"
                          inputMode="decimal"
                          className="h-8 tabular-nums"
                          disabled={saving}
                          value={formatMinorToEditableMajor(r.cap_amount, amountCurrencyCode)}
                          placeholder={t("riskCap.placeholders.capAmount", { ns: "config" })}
                          onChange={(e) =>
                            updateRow(idx, {
                              cap_amount: parseAdminMajorToMinor(e.target.value, amountCurrencyCode) ?? 0,
                            })
                          }
                        />
                      ) : (
                        <ConfigReadonlyValue>
                          {formatAdminMinorDecimal(r.cap_amount, amountCurrencyCode)}
                        </ConfigReadonlyValue>
                      )}
                    </TableCell>
                    <TableCell className="sticky right-0 z-10 bg-card text-center shadow-[-1px_0_0_rgba(203,213,225,0.7)]">
                      {canEditDraft ? (
                        <AdminRowActionsMenu
                          busy={saving}
                          actions={[
                            {
                              key: "delete",
                              label: t("actions.delete", { ns: "adminUsers" }),
                              icon: Trash2,
                              destructive: true,
                              onClick: () => removeRow(idx),
                            },
                          ]}
                        />
                      ) : (
                        <span className="text-sm text-muted-foreground">—</span>
                      )}
                          </TableCell>
                        </TableRow>
                      ))}
                    </TableBody>
                  </Table>
                </div>
              ) : null}

              {drawBoundRows.length > 0 ? (
                <div className="admin-table-shell">
                  <Table>
                    <TableHeader>
                      <TableRow>
                        <TableHead>{t("riskCap.table.draw", { ns: "config" })}</TableHead>
                        <TableHead>{t("riskCap.table.number", { ns: "config" })}</TableHead>
                        <TableHead>{t("riskCap.table.capAmount", { ns: "config" })}</TableHead>
                      </TableRow>
                    </TableHeader>
                    <TableBody>
                      {drawBoundRows.map((row) => (
                        <TableRow key={row.clientKey}>
                          <TableCell>{drawLabel(row.draw_id ?? 0)}</TableCell>
                          <TableCell className="font-mono">{row.normalized_number}</TableCell>
                          <TableCell>{formatAdminMinorDecimal(row.cap_amount, amountCurrencyCode)}</TableCell>
                        </TableRow>
                      ))}
                    </TableBody>
                  </Table>
                </div>
              ) : null}
            </div>
          )}
        </AdminPageCard>
      ) : null}

      <Dialog open={rollbackOpen} onOpenChange={setRollbackOpen}>
        <DialogContent showCloseButton className="sm:max-w-md">
          <DialogHeader>
            <DialogTitle>{t("versionActions.rollbackDialog.title", { ns: "config" })}</DialogTitle>
            <DialogDescription>
              {t("versionActions.rollbackDialog.description", {
                ns: "config",
                version: rollbackTarget?.version_no ?? "—",
              })}
            </DialogDescription>
          </DialogHeader>
          <DialogFooter>
            <Button type="button" variant="outline" onClick={() => setRollbackOpen(false)}>
              {t("actions.cancel", { ns: "adminUsers" })}
            </Button>
            <Button type="button" onClick={() => void handleRollback()} disabled={!rollbackTarget || saving}>
              {t("versionActions.rollbackDialog.confirm", { ns: "config" })}
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>

      <ConfirmDialog />
    </ConfigDocPage>
  );
}