import { adminRequest } from "@/lib/admin-http";


import type {
  AdminPlayerWalletsData,
  AdminTransferOrderListData,
  AdminWalletTxnListData,
} from "@/types/api/admin-wallet";

const A = `/admin`;

export type TransferOrderListQuery = {
  page?: number;
  per_page?: number;
  player_id?: number;
  /** 模糊：site_player_id / username */
  player_account?: string;
  transfer_no?: string;
  external_ref_no?: string;
  created_from?: string;
  created_to?: string;
  status?: string;
  /** 仅异常：processing / failed / pending_reconcile */
  abnormal?: boolean;
  site_code?: string;
  agent_node_id?: number;
};

export async function getAdminTransferOrders(
  q: TransferOrderListQuery = {},
): Promise<AdminTransferOrderListData> {
  return adminRequest.get<AdminTransferOrderListData>(
    `${A}/wallet/transfer-orders`,
    { params: q },
  );
}

export type WalletTransactionListQuery = {
  page?: number;
  per_page?: number;
  player_id?: number;
  player_account?: string;
  txn_no?: string;
  external_ref_no?: string;
  created_from?: string;
  created_to?: string;
  biz_type?: string;
  status?: string;
  abnormal?: boolean;
  site_code?: string;
  agent_node_id?: number;
};

export async function getAdminWalletTransactions(
  q: WalletTransactionListQuery = {},
): Promise<AdminWalletTxnListData> {
  return adminRequest.get<AdminWalletTxnListData>(
    `${A}/wallet/transactions`,
    { params: q },
  );
}

export async function getAdminPlayerWallets(
  playerId: number,
): Promise<AdminPlayerWalletsData> {
  return adminRequest.get<AdminPlayerWalletsData>(
    `${A}/players/${playerId}/wallets`,
  );
}

export type TransferOrderActionResult = {
  transfer_no: string;
  status: string;
};

export async function reverseTransferOrder(
  transferNo: string,
  remark?: string,
): Promise<TransferOrderActionResult> {
  return adminRequest.post<TransferOrderActionResult>(
    `${A}/wallet/transfer-orders/${transferNo}/reverse`,
    remark ? { remark } : {},
  );
}

export async function manuallyProcessTransferOrder(
  transferNo: string,
  remark?: string,
): Promise<TransferOrderActionResult> {
  return adminRequest.post<TransferOrderActionResult>(
    `${A}/wallet/transfer-orders/${transferNo}/manually-process`,
    remark ? { remark } : {},
  );
}

export async function completeTransferInCredit(
  transferNo: string,
  remark?: string,
): Promise<TransferOrderActionResult> {
  return adminRequest.post<TransferOrderActionResult>(
    `${A}/wallet/transfer-orders/${transferNo}/complete-credit`,
    remark ? { remark } : {},
  );
}
