import { ref, computed } from 'vue';
import { ElMessage } from 'element-plus';
import api from '../api';
import { useAdminLocale } from './useAdminLocale';
import { resolveApiError } from '../i18n/form-validation';
import {
  depositAmountCap,
  parsePlayerAvailable,
  type WalletTransferContext,
} from '../utils/wallet-transfer-context';

export type PlayerTransferTarget = { id: string; username?: string };

export function useAdminPlayerTransfer(onSuccess?: () => void | Promise<void>) {
  const { t } = useAdminLocale();

  const transferVisible = ref(false);
  const transferLoading = ref(false);
  const transferType = ref<'deposit' | 'withdraw'>('deposit');
  const transferTarget = ref<PlayerTransferTarget | null>(null);
  const transferAmount = ref(100);
  const transferRemark = ref('');
  const transferContext = ref<WalletTransferContext | null>(null);
  const transferContextLoading = ref(false);

  const transferAmountRange = computed(() => {
    if (transferType.value === 'withdraw') {
      const cap = parsePlayerAvailable(transferContext.value);
      if (cap <= 0) return { min: 0, max: 0 };
      return { min: Math.min(0.01, cap), max: cap };
    }
    const cap = depositAmountCap(transferContext.value);
    if (cap === undefined) return { min: 0.01, max: undefined as number | undefined };
    if (cap <= 0) return { min: 0, max: 0 };
    return { min: Math.min(0.01, cap), max: cap };
  });

  const transferAmountDisabled = computed(() => transferAmountRange.value.max === 0);

  /** 有上限时超出额度（勿用 el-input-number :max，否则会静默截断到上限） */
  const transferAmountExceedsCap = computed(() => {
    const max = transferAmountRange.value.max;
    if (max === undefined) return false;
    return transferAmount.value > max;
  });

  const transferAmountCapError = computed(() => {
    if (!transferAmountExceedsCap.value) return '';
    return transferType.value === 'deposit'
      ? t('err.insufficient_credit')
      : t('transfer.context.withdraw_exceed');
  });

  function transferTitle() {
    const name = transferTarget.value?.username ?? transferTarget.value?.id ?? '';
    return transferType.value === 'deposit'
      ? t('agent_portal.transfer_title_deposit', { name })
      : t('agent_portal.transfer_title_withdraw', { name });
  }

  async function openTransfer(type: 'deposit' | 'withdraw', row: PlayerTransferTarget) {
    transferType.value = type;
    transferTarget.value = row;
    transferContext.value = null;
    transferAmount.value = 100;
    transferRemark.value = type === 'deposit' ? t('user.deposit_remark_default') : t('user.withdraw_remark_default');
    transferVisible.value = true;
    transferContextLoading.value = true;
    try {
      const { data } = await api.get(`/admin/wallet/transfer-context/${row.id}`);
      transferContext.value = data.data as WalletTransferContext;
      if (type === 'deposit') {
        const cap = depositAmountCap(transferContext.value);
        transferAmount.value =
          cap !== undefined && cap > 0 ? Math.min(100, cap) : cap === undefined ? 100 : 0;
      } else {
        const cap = parsePlayerAvailable(transferContext.value);
        transferAmount.value = cap > 0 ? Math.min(100, cap) : 0;
      }
    } catch (e: unknown) {
      ElMessage.error(resolveApiError(e, t, 'msg.load_failed'));
      transferVisible.value = false;
    } finally {
      transferContextLoading.value = false;
    }
  }

  async function submitTransfer() {
    if (!transferTarget.value) return;
    if (transferAmount.value <= 0) {
      ElMessage.warning(t('msg.amount_gt_zero'));
      return;
    }
    const max = transferAmountRange.value.max;
    if (max !== undefined && transferAmount.value > max) {
      ElMessage.warning(
        transferType.value === 'deposit'
          ? t('err.insufficient_credit')
          : t('transfer.context.withdraw_exceed'),
      );
      return;
    }
    const userId = transferTarget.value.id;
    const amount = transferAmount.value;
    transferLoading.value = true;
    try {
      const requestId = `${transferType.value === 'deposit' ? 'dep' : 'wd'}-${userId}-${Date.now()}`;
      const endpoint =
        transferType.value === 'deposit' ? '/admin/wallet/deposit' : '/admin/wallet/withdraw';
      await api.post(endpoint, {
        userId,
        amount,
        remark: transferRemark.value || undefined,
        requestId,
      });
      ElMessage.success(
        transferType.value === 'deposit' ? t('msg.topup_ok') : t('msg.withdraw_ok'),
      );
      transferVisible.value = false;
      await onSuccess?.();
    } catch (e: unknown) {
      ElMessage.error(resolveApiError(e, t, transferType.value === 'deposit' ? 'msg.topup_failed' : 'msg.transfer_failed'));
    } finally {
      transferLoading.value = false;
    }
  }

  return {
    transferVisible,
    transferLoading,
    transferType,
    transferTarget,
    transferAmount,
    transferRemark,
    transferContext,
    transferContextLoading,
    transferAmountRange,
    transferAmountDisabled,
    transferAmountExceedsCap,
    transferAmountCapError,
    transferTitle,
    openTransfer,
    submitTransfer,
  };
}
