"use client";

import { useCallback, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";

import {
  getAdminJackpotPoolAdjustments,
  getAdminJackpotPools,
  postAdminJackpotManualBurst,
  postAdminJackpotPoolAdjustment,
  putAdminJackpotPool,
} from "@/api/admin-jackpot";
import { useConfirmAction } from "@/hooks/use-confirm-action";
import { adminHasAnyPermission } from "@/lib/admin-permissions";
import { formatAdminMinorDecimal, parseAdminMajorToMinor } from "@/lib/money";
import { PRD_JACKPOT_MANAGE, PRD_JACKPOT_MANUAL_BURST } from "@/lib/admin-prd";
import { ModuleScaffold } from "@/components/admin/module-scaffold";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { toast } from "sonner";
import { useAdminProfile } from "@/stores/admin-session";
import { LotteryApiBizError } from "@/types/api/errors";
import type { AdminJackpotPoolAdjustmentRow, AdminJackpotPoolRow } from "@/types/api/admin-jackpot";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";

type Draft = {
  contribution_rate: string;
  trigger_threshold: string;
  payout_rate: string;
  force_trigger_draw_gap: string;
  min_bet_amount: string;
  combo_trigger_play_codes: string;
  status: string;
  manual_burst_draw_id: string;
};

type AdjustmentDraft = {
  direction: "increase" | "decrease";
  amount: string;
  reason: string;
};

function toDraft(p: AdminJackpotPoolRow): Draft {
  return {
    contribution_rate: String(p.contribution_rate),
    trigger_threshold: formatAdminMinorDecimal(p.trigger_threshold, p.currency_code),
    payout_rate: String(p.payout_rate),
    force_trigger_draw_gap: String(p.force_trigger_draw_gap),
    min_bet_amount: formatAdminMinorDecimal(p.min_bet_amount, p.currency_code),
    combo_trigger_play_codes: p.combo_trigger_play_codes.join(","),
    status: String(p.status),
    manual_burst_draw_id: "",
  };
}

type JackpotPoolsConsoleProps = {
  /** 嵌入运营配置单页时去掉外层脚手架与重复标题 */
  embedded?: boolean;
};

export function JackpotPoolsConsole({ embedded = false }: JackpotPoolsConsoleProps) {
  const { t } = useTranslation(["jackpot", "common"]);
  const profile = useAdminProfile();
  const canManageJackpot = adminHasAnyPermission(profile?.permissions, [PRD_JACKPOT_MANAGE]);
  const canManualBurst = adminHasAnyPermission(profile?.permissions, [PRD_JACKPOT_MANUAL_BURST]);
  const { request: requestConfirm, ConfirmDialog: ConfirmActionDialog } = useConfirmAction();
  const [items, setItems] = useState<AdminJackpotPoolRow[]>([]);
  const [drafts, setDrafts] = useState<Record<number, Draft>>({});
  const [loading, setLoading] = useState(true);
  const [savingId, setSavingId] = useState<number | null>(null);
  const [burstingId, setBurstingId] = useState<number | null>(null);
  const [confirmBurstPoolId, setConfirmBurstPoolId] = useState<number | null>(null);
  const [adjustmentDrafts, setAdjustmentDrafts] = useState<Record<number, AdjustmentDraft>>({});
  const [adjustmentRows, setAdjustmentRows] = useState<Record<number, AdminJackpotPoolAdjustmentRow[]>>({});
  const [adjustingId, setAdjustingId] = useState<number | null>(null);

  const load = useCallback(async () => {
    setLoading(true);
    try {
      const res = await getAdminJackpotPools();
      setItems(res.items);
      const d: Record<number, Draft> = {};
      const adjDrafts: Record<number, AdjustmentDraft> = {};
      const adjRows: Record<number, AdminJackpotPoolAdjustmentRow[]> = {};
      for (const p of res.items) {
        d[p.id] = toDraft(p);
        adjDrafts[p.id] = { direction: "increase", amount: "", reason: "" };
        try {
          const ledger = await getAdminJackpotPoolAdjustments(p.id, { per_page: 5 });
          adjRows[p.id] = ledger.items;
        } catch {
          adjRows[p.id] = [];
        }
      }
      setDrafts(d);
      setAdjustmentDrafts(adjDrafts);
      setAdjustmentRows(adjRows);
    } catch (e) {
      toast.error(e instanceof LotteryApiBizError ? e.message : t("loadFailed"));
    } finally {
      setLoading(false);
    }
  }, [t]);

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

  const updateDraft = (id: number, patch: Partial<Draft>) => {
    setDrafts((prev) => ({
      ...prev,
      [id]: { ...prev[id], ...patch },
    }));
  };

  const updateAdjustmentDraft = (id: number, patch: Partial<AdjustmentDraft>) => {
    setAdjustmentDrafts((prev) => ({
      ...prev,
      [id]: { ...(prev[id] ?? { direction: "increase", amount: "", reason: "" }), ...patch },
    }));
  };

  const save = async (p: AdminJackpotPoolRow) => {
    const d = drafts[p.id];
    if (!d) return;
    setSavingId(p.id);
    try {
      await putAdminJackpotPool(p.id, {
        contribution_rate: Number(d.contribution_rate),
        trigger_threshold: parseAdminMajorToMinor(d.trigger_threshold, p.currency_code) ?? 0,
        payout_rate: Number(d.payout_rate),
        force_trigger_draw_gap: Number.parseInt(d.force_trigger_draw_gap, 10),
        min_bet_amount: parseAdminMajorToMinor(d.min_bet_amount, p.currency_code) ?? 0,
        combo_trigger_play_codes: d.combo_trigger_play_codes
          .split(",")
          .map((v) => v.trim().toLowerCase())
          .filter(Boolean),
        status: Number.parseInt(d.status, 10),
      });
      toast.success(t("saveSuccess"));
      await load();
    } catch (e) {
      toast.error(e instanceof LotteryApiBizError ? e.message : t("saveFailed"));
    } finally {
      setSavingId(null);
    }
  };

  const submitAdjustment = async (p: AdminJackpotPoolRow) => {
    const adj = adjustmentDrafts[p.id];
    if (!adj) return;
    const minor = parseAdminMajorToMinor(adj.amount, p.currency_code);
    if (minor === null || minor <= 0) {
      toast.error(t("adjustmentAmountInvalid"));
      return;
    }
    const trimmedReason = adj.reason.trim();
    if (trimmedReason.length < 3) {
      toast.error(t("adjustmentReasonRequired"));
      return;
    }
    const amountDelta = adj.direction === "decrease" ? -minor : minor;

    setAdjustingId(p.id);
    try {
      const res = await postAdminJackpotPoolAdjustment(p.id, {
        amount_delta: amountDelta,
        reason: trimmedReason,
      });
      toast.success(t("adjustmentSuccess"));
      setItems((prev) =>
        prev.map((row) =>
          row.id === p.id ? { ...row, current_amount: res.pool.current_amount } : row,
        ),
      );
      updateAdjustmentDraft(p.id, { amount: "", reason: "" });
      const ledger = await getAdminJackpotPoolAdjustments(p.id, { per_page: 5 });
      setAdjustmentRows((prev) => ({ ...prev, [p.id]: ledger.items }));
    } catch (e) {
      toast.error(e instanceof LotteryApiBizError ? e.message : t("adjustmentFailed"));
    } finally {
      setAdjustingId(null);
    }
  };

  const manualBurst = async (p: AdminJackpotPoolRow) => {
    const d = drafts[p.id];
    if (!d) return;
    const drawId = Number.parseInt(d.manual_burst_draw_id, 10);
    if (!Number.isFinite(drawId) || drawId <= 0) {
      toast.error(t("invalidDrawId"));
      return;
    }

    setBurstingId(p.id);
    try {
      const res = await postAdminJackpotManualBurst(p.id, { draw_id: drawId });
      toast.success(
        `${t("manualBurstSuccess")} · ${res.draw_no} · ${res.winner_count} ${t("winnerCount")}`,
      );
      await load();
    } catch (e) {
      toast.error(e instanceof LotteryApiBizError ? e.message : t("manualBurstFailed"));
    } finally {
      setBurstingId(null);
      setConfirmBurstPoolId(null);
    }
  };

  const poolList = (
    <div className={embedded ? "space-y-4" : "space-y-8"}>
      {loading ? <p className="text-muted-foreground text-sm">{t("states.loading", { ns: "common" })}</p> : null}
      {!loading && items.length === 0 ? (
        <p className="text-muted-foreground text-sm">{t("noPoolData")}</p>
      ) : null}
      {items.map((p) => {
        const d = drafts[p.id] ?? toDraft(p);
        const adj = adjustmentDrafts[p.id] ?? { direction: "increase", amount: "", reason: "" };
        const ledger = adjustmentRows[p.id] ?? [];
        return (
          <div
            key={p.id}
            className="space-y-4 rounded-xl border border-border/60 bg-muted/10 p-4"
          >
            <div className="flex flex-wrap items-baseline justify-between gap-2">
              <h3 className="font-mono text-sm font-semibold">{p.currency_code}</h3>
              <p className="text-muted-foreground font-mono text-xs">
                {t("displayBalance", {
                  amount: formatAdminMinorDecimal(p.current_amount, p.currency_code),
                })}
              </p>
            </div>
            {canManageJackpot ? (
              <div className="space-y-3 rounded-lg border border-border/60 bg-background/60 p-4">
                <p className="text-sm font-medium">{t("balanceAdjustmentTitle")}</p>
                <p className="text-muted-foreground text-xs">{t("balanceAdjustmentHint")}</p>
                <div className="grid gap-4 sm:grid-cols-2 xl:grid-cols-4">
                  <div className="space-y-1.5">
                    <Label>{t("adjustmentDirection")}</Label>
                    <Select
                      value={adj.direction}
                      onValueChange={(value: "increase" | "decrease") =>
                        updateAdjustmentDraft(p.id, { direction: value })
                      }
                    >
                      <SelectTrigger className="font-mono">
                        <SelectValue />
                      </SelectTrigger>
                      <SelectContent>
                        <SelectItem value="increase">{t("adjustmentIncrease")}</SelectItem>
                        <SelectItem value="decrease">{t("adjustmentDecrease")}</SelectItem>
                      </SelectContent>
                    </Select>
                  </div>
                  <div className="space-y-1.5">
                    <Label htmlFor={`adj-amt-${p.id}`}>{t("adjustmentAmount")}</Label>
                    <Input
                      id={`adj-amt-${p.id}`}
                      className="font-mono"
                      value={adj.amount}
                      onChange={(e) => updateAdjustmentDraft(p.id, { amount: e.target.value })}
                    />
                  </div>
                  <div className="space-y-1.5 sm:col-span-2 xl:col-span-2">
                    <Label htmlFor={`adj-reason-${p.id}`}>{t("adjustmentReason")}</Label>
                    <Textarea
                      id={`adj-reason-${p.id}`}
                      rows={2}
                      value={adj.reason}
                      onChange={(e) => updateAdjustmentDraft(p.id, { reason: e.target.value })}
                    />
                  </div>
                </div>
                <div className="flex justify-end">
                  <Button
                    type="button"
                    variant="secondary"
                    disabled={adjustingId === p.id}
                    onClick={() =>
                      requestConfirm({
                        title: t("confirmAdjustmentTitle"),
                        description: t("confirmAdjustmentDescription"),
                        confirmLabel: t("confirm.confirmSave", { ns: "common" }),
                        onConfirm: () => submitAdjustment(p),
                      })
                    }
                  >
                    {adjustingId === p.id ? t("processing") : t("submitAdjustment")}
                  </Button>
                </div>
                {ledger.length > 0 ? (
                  <div className="space-y-2 border-t border-border/60 pt-3">
                    <p className="text-xs font-medium">{t("recentAdjustments")}</p>
                    <ul className="text-muted-foreground space-y-1 font-mono text-xs">
                      {ledger.map((row) => (
                        <li key={row.id}>
                          {row.adjustment_no} · {row.amount_delta > 0 ? "+" : ""}
                          {formatAdminMinorDecimal(row.amount_delta, p.currency_code)} ·{" "}
                          {row.reason}
                        </li>
                      ))}
                    </ul>
                  </div>
                ) : null}
              </div>
            ) : null}
            <fieldset disabled={!canManageJackpot} className="grid gap-4 border-0 p-0 sm:grid-cols-2 xl:grid-cols-4">
              <div className="space-y-1.5">
                <Label htmlFor={`cr-${p.id}`}>{t("contributionRate")}</Label>
                <Input
                  id={`cr-${p.id}`}
                  className="font-mono"
                  value={d.contribution_rate}
                  onChange={(e) => updateDraft(p.id, { contribution_rate: e.target.value })}
                />
              </div>
              <div className="space-y-1.5">
                <Label htmlFor={`th-${p.id}`}>{t("triggerThreshold")}</Label>
                <Input
                  id={`th-${p.id}`}
                  className="font-mono"
                  value={d.trigger_threshold}
                  onChange={(e) => updateDraft(p.id, { trigger_threshold: e.target.value })}
                />
              </div>
              <div className="space-y-1.5">
                <Label htmlFor={`pr-${p.id}`}>{t("payoutRate")}</Label>
                <Input
                  id={`pr-${p.id}`}
                  className="font-mono"
                  value={d.payout_rate}
                  onChange={(e) => updateDraft(p.id, { payout_rate: e.target.value })}
                />
              </div>
              <div className="space-y-1.5">
                <Label htmlFor={`gap-${p.id}`}>{t("forceTriggerGap")}</Label>
                <Input
                  id={`gap-${p.id}`}
                  className="font-mono"
                  value={d.force_trigger_draw_gap}
                  onChange={(e) => updateDraft(p.id, { force_trigger_draw_gap: e.target.value })}
                />
              </div>
              <div className="space-y-1.5">
                <Label htmlFor={`min-${p.id}`}>{t("minBetAmount")}</Label>
                <Input
                  id={`min-${p.id}`}
                  className="font-mono"
                  value={d.min_bet_amount}
                  onChange={(e) => updateDraft(p.id, { min_bet_amount: e.target.value })}
                />
              </div>
              <div className="space-y-1.5">
                <Label htmlFor={`combo-${p.id}`}>{t("comboTriggerPlays")}</Label>
                <Input
                  id={`combo-${p.id}`}
                  className="font-mono"
                  value={d.combo_trigger_play_codes}
                  placeholder="straight,ibox"
                  onChange={(e) => updateDraft(p.id, { combo_trigger_play_codes: e.target.value })}
                />
              </div>
              <div className="flex items-center justify-between gap-3 rounded-lg border border-border/60 px-3 py-2.5">
                <Label htmlFor={`status-${p.id}`} className="text-sm font-medium">
                  {t("status")}
                </Label>
                <Switch
                  id={`status-${p.id}`}
                  checked={d.status === "1"}
                  disabled={!canManageJackpot}
                  aria-label={t("status")}
                  onCheckedChange={(checked) =>
                    updateDraft(p.id, { status: checked ? "1" : "0" })
                  }
                />
              </div>
            </fieldset>
            {canManageJackpot ? (
              <div className="flex justify-end border-t border-border/60 pt-3">
                <Button
                  type="button"
                  disabled={savingId === p.id}
                  onClick={() =>
                    requestConfirm({
                      title: t("confirmSavePoolTitle"),
                      description: t("confirmSavePoolDescription"),
                      confirmLabel: t("confirm.confirmSave", { ns: "common" }),
                      onConfirm: () => save(p),
                    })
                  }
                >
                  {savingId === p.id ? t("saving") : t("save")}
                </Button>
              </div>
            ) : null}
            {canManualBurst ? (
            <div className="rounded-lg border border-amber-200/80 bg-amber-50/80 p-4 dark:border-amber-900/50 dark:bg-amber-950/30">
              <p className="mb-1 text-xs font-medium text-amber-900 dark:text-amber-200">
                {t("manualBurst")}
              </p>
              <p className="mb-3 text-xs text-amber-800/90 dark:text-amber-300/90">{t("manualBurstHint")}</p>
              <div className="flex flex-col gap-3 sm:flex-row sm:flex-wrap sm:items-end">
                <div className="min-w-0 flex-1 space-y-1.5 sm:max-w-xs">
                  <Label htmlFor={`burst-draw-${p.id}`}>{t("manualBurstDrawId")}</Label>
                  <Input
                    id={`burst-draw-${p.id}`}
                    className="font-mono"
                    value={d.manual_burst_draw_id}
                    onChange={(e) => updateDraft(p.id, { manual_burst_draw_id: e.target.value })}
                  />
                </div>
                <Button
                  type="button"
                  variant="destructive"
                  className="shrink-0 sm:ml-auto"
                  disabled={burstingId === p.id}
                  onClick={() => setConfirmBurstPoolId(p.id)}
                >
                  {burstingId === p.id ? t("processing") : t("manualBurst")}
                </Button>
              </div>
            </div>
            ) : null}
          </div>
        );
      })}
    </div>
  );

  const confirmPool = confirmBurstPoolId !== null ? items.find((p) => p.id === confirmBurstPoolId) : null;
  const confirmDraft = confirmPool ? drafts[confirmPool.id] : null;

  const confirmDialog = (
    <Dialog open={confirmBurstPoolId !== null} onOpenChange={(open) => !open && setConfirmBurstPoolId(null)}>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>{t("manualBurstConfirmTitle")}</DialogTitle>
          <DialogDescription>
            {t("manualBurstConfirmDescription", {
              drawId: confirmDraft?.manual_burst_draw_id ?? "—",
            })}
          </DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <Button type="button" variant="outline" onClick={() => setConfirmBurstPoolId(null)}>
            {t("cancel")}
          </Button>
          <Button
            type="button"
            variant="destructive"
            disabled={confirmPool === undefined || burstingId !== null}
            onClick={() => confirmPool && void manualBurst(confirmPool)}
          >
            {t("manualBurstConfirm")}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );

  if (embedded) {
    return (
      <>
        {poolList}
        {confirmDialog}
        <ConfirmActionDialog />
      </>
    );
  }

  return (
    <ModuleScaffold>
      <Card>
        <CardHeader>
          <CardTitle className="text-base">{t("configTitle")}</CardTitle>
        </CardHeader>
        <CardContent>{poolList}</CardContent>
      </Card>
      {confirmDialog}
      <ConfirmActionDialog />
    </ModuleScaffold>
  );
}
