import api, { PageResponse, unwrap } from './request';

export interface WithdrawRequestRow extends Record<string, unknown> {
  withdrawId: number;
  agentId: number;
  agentName: string;
  settleMonth: string;
  requestAmount: number;
  bankCode: string;
  accountNo: string;
  accountHolder: string;
  status: string;          // PENDING / IN_APPROVAL / APPROVED / SENT / COMPLETED / FAILED
  approvalRequestId?: number;
  approvalStatus?: string;
  requestedAt: string;
  completedAt?: string;
  failReason?: string;
  transferTxId?: string;
  transferStatus?: string;     // ACCEPTED / SETTLED / FAILED
  transferAt?: string;
}

export interface WithdrawRequestSaveReq {
  settleMonth: string;
  requestAmount: number;
  bankCode: string;
  accountNo: string;
  accountHolder: string;
}

export interface WithdrawSearchParam {
  agentId?: number;
  agentName?: string;
  settleMonth?: string;
  status?: string;
  viewAll?: boolean;
  pageNum?: number;
  pageSize?: number;
}

export const withdrawApi = {
  list: (p: WithdrawSearchParam) =>
    unwrap<PageResponse<WithdrawRequestRow>>(
      api.get('/api/withdraw-requests', { params: p }),
    ),
  create: (body: WithdrawRequestSaveReq) =>
    unwrap<WithdrawRequestRow>(api.post('/api/withdraw-requests', body)),
};
