/**
 * 分销功能相关API
 */

import { get, post } from '@/utils/request';

export enum WithdrawalStatus {
  PENDING = 'pending',
  APPROVED = 'approved',
  REJECTED = 'rejected',
  PAID = 'paid'
}

export enum WithdrawalStatusLabel {
  PENDING = '审核中',
  APPROVED = '已通过',
  REJECTED = '已驳回',
  PAID = '已打款'
}

export interface DistributionStats {
  totalReferrals: number;
  validReferrals: number;
  commissionBalance: number;
  frozenAmount: number;
  totalCommission: number;
  totalWithdrawn: number;
}

export interface WithdrawalRecord {
  id: string;
  amount: number;
  status: WithdrawalStatus;
  createTime: string;
  auditTime?: string;
  payTime?: string;
  rejectReason?: string;
}

export interface WithdrawalListResponse {
  list: WithdrawalRecord[];
  total: number;
  page: number;
  pageSize: number;
}

export interface WithdrawalQueryParams {
  page?: number;
  pageSize?: number;
  status?: WithdrawalStatus;
}

/**
 * 绑定分销员
 */
export async function bindDistributor(distributorCode: string): Promise<{ success: boolean; message: string }> {
  return post('/distribution/bind', { distributorCode });
}

/**
 * 获取我的分销统计
 */
export async function getMyStats(): Promise<DistributionStats> {
  return get('/distribution/my-stats');
}

/**
 * 申请提现
 */
export async function applyWithdrawal(amount: number): Promise<{ success: boolean; message: string }> {
  return post('/distribution/withdrawal', { amount });
}

/**
 * 获取提现记录列表
 */
export async function getMyWithdrawalList(params: WithdrawalQueryParams = {}): Promise<WithdrawalListResponse> {
  return get('/distribution/withdrawal/list', params);
}
