"use client";

import Link from "next/link";
import { ExternalLink, RotateCcw, Wrench } from "lucide-react";
import { useTranslation } from "react-i18next";

import { AdminRowActionsMenu } from "@/components/admin/admin-row-actions-menu";
import { buttonVariants } from "@/components/ui/button";
import {
  canCompleteTransferInCredit,
  canManuallyProcessTransferOrder,
  canReverseTransferOrder,
  transferOrderHasReconcileAction,
} from "@/lib/wallet-transfer-actions";
import { cn } from "@/lib/utils";
import type { AdminReconcileItemRow } from "@/types/api/admin-reconcile";

type ReconcileItemActionsProps = {
  row: AdminReconcileItemRow;
  canWriteWallet: boolean;
  busy: boolean;
  compact?: boolean;
  onCompleteCredit: (transferNo: string) => void;
  onReverse: (transferNo: string) => void;
  onManualProcess: (transferNo: string) => void;
};

export function ReconcileItemActions({
  row,
  canWriteWallet,
  busy,
  compact = false,
  onCompleteCredit,
  onReverse,
  onManualProcess,
}: ReconcileItemActionsProps): React.ReactElement {
  const { t } = useTranslation(["reconcile", "wallet"]);

  const transferNo = row.side_a_ref ?? "";
  const actionRow = {
    direction: row.transfer_direction ?? undefined,
    status: row.current_transfer_status ?? row.status,
    fail_reason: row.transfer_fail_reason,
    external_ref_no: row.external_ref_no,
    can_reverse: row.can_reverse,
    can_complete_credit: row.can_complete_credit,
    can_manually_process: row.can_manually_process,
  };

  const hasWalletAction = transferNo !== "" && transferOrderHasReconcileAction(actionRow, canWriteWallet);

  const menuActions = [
    {
      key: "complete",
      label: t("completeCredit", { ns: "wallet" }),
      hidden: !canCompleteTransferInCredit(actionRow, canWriteWallet),
      onClick: () => onCompleteCredit(transferNo),
    },
    {
      key: "manual",
      label: t("markCaseClosed", { ns: "wallet" }),
      icon: Wrench,
      hidden: !canManuallyProcessTransferOrder(actionRow, canWriteWallet),
      onClick: () => onManualProcess(transferNo),
    },
    {
      key: "reverse",
      label: t("reverse", { ns: "wallet" }),
      icon: RotateCcw,
      destructive: true,
      hidden: !canReverseTransferOrder(actionRow, canWriteWallet),
      onClick: () => onReverse(transferNo),
    },
    {
      key: "transfer",
      label: t("openTransferOrder"),
      icon: ExternalLink,
      hidden: transferNo === "",
      onClick: () => {
        window.location.href = `/admin/wallet/transfer-orders?transfer_no=${encodeURIComponent(transferNo)}`;
      },
    },
    {
      key: "txn",
      label: t("openWalletTxn"),
      icon: ExternalLink,
      hidden: !row.side_b_ref,
      onClick: () => {
        window.location.href = `/admin/wallet/transactions?txn_no=${encodeURIComponent(row.side_b_ref ?? "")}`;
      },
    },
  ];

  if (compact) {
    if (!hasWalletAction && !transferNo && !row.side_b_ref) {
      return <span>—</span>;
    }
    return <AdminRowActionsMenu busy={busy} actions={menuActions} />;
  }

  return (
    <div className="flex flex-wrap gap-2">
      {hasWalletAction ? <AdminRowActionsMenu busy={busy} actions={menuActions.filter((a) => a.key !== "transfer" && a.key !== "txn")} /> : null}
      {transferNo !== "" ? (
        <Link
          href={`/admin/wallet/transfer-orders?transfer_no=${encodeURIComponent(transferNo)}`}
          className={cn(buttonVariants({ variant: "outline", size: "sm" }), "h-8")}
        >
          {t("openTransferOrder")}
        </Link>
      ) : null}
      {row.side_b_ref ? (
        <Link
          href={`/admin/wallet/transactions?txn_no=${encodeURIComponent(row.side_b_ref)}`}
          className={cn(buttonVariants({ variant: "outline", size: "sm" }), "h-8")}
        >
          {t("openWalletTxn")}
        </Link>
      ) : null}
      {!transferNo && !row.side_b_ref ? <span>—</span> : null}
    </div>
  );
}