import { requestClient } from '#/api/request';

export namespace AgentApi {
  export interface AgentListItem {
    id: number;
    user_id: number;
    level_name: string;
    region: string;
    mobile: string;
    membership_expiry_time: string;
    balance: number;
    total_earnings: number;
    frozen_balance: number;
    withdrawn_amount: number;
    create_time: string;
    is_real_name_verified: boolean;
    real_name: string;
    id_card: string;
    real_name_status: 'approved' | 'pending' | 'rejected';
  }

  export interface AgentList {
    total: number;
    items: AgentListItem[];
  }

  export interface GetAgentListParams {
    page: number;
    pageSize: number;
    mobile?: string;
    region?: string;
    parent_agent_id?: number;
    id?: number;
    create_time_start?: string;
    create_time_end?: string;
    order_by?: string;
    order_type?: 'asc' | 'desc';
  }

  export interface AgentLinkListItem {
    agent_id: number;
    product_name: string;
    price: number;
    link_identifier: string;
    create_time: string;
  }

  export interface AgentLinkList {
    total: number;
    items: AgentLinkListItem[];
  }

  export interface GetAgentLinkListParams {
    page: number;
    pageSize: number;
    agent_id?: number;
    product_name?: string;
    link_identifier?: string;
  }

  // 代理佣金相关接口
  export interface AgentCommissionListItem {
    id: number;
    agent_id: number;
    order_id: number;
    amount: number;
    product_name: string;
    status: number;
    create_time: string;
  }

  export interface AgentCommissionList {
    total: number;
    items: AgentCommissionListItem[];
  }

  export interface GetAgentCommissionListParams {
    page: number;
    pageSize: number;
    agent_id?: number;
    order_id?: number;
    product_name?: string;
    status?: number;
  }

  // 代理奖励相关接口
  export interface AgentRewardListItem {
    id: number;
    agent_id: number;
    relation_agent_id: number;
    amount: number;
    type: string;
    create_time: string;
  }

  export interface AgentRewardList {
    total: number;
    items: AgentRewardListItem[];
  }

  export interface GetAgentRewardListParams {
    page: number;
    pageSize: number;
    agent_id?: number;
    relation_agent_id?: number;
    type?: string;
  }

  // 代理提现相关接口
  export interface AgentWithdrawalListItem {
    id: number;
    agent_id: number;
    withdraw_no: string;
    amount: number;
    actual_amount: number; // 实际到账金额（扣税后）
    tax_amount: number; // 扣税金额
    status: number;
    payee_account: string;
    remark: string;
    create_time: string;
    withdraw_type: number; // 提现类型:1-支付宝,2-银行卡
    bank_card_no?: string; // 银行卡号
    bank_name?: string; // 开户支行
    payee_name?: string; // 收款人姓名
  }

  export interface AgentWithdrawalList {
    total: number;
    items: AgentWithdrawalListItem[];
  }

  export interface GetAgentWithdrawalListParams {
    page: number;
    pageSize: number;
    agent_id?: number;
    status?: number;
    withdraw_no?: string;
  }

  // 提现统计数据
export interface WithdrawalStatistics {
  total_withdrawal_amount: number;
  today_withdrawal_amount: number;
  total_actual_amount: number;
  total_tax_amount: number;
}

// 代理订单统计数据
export interface AgentOrderStatistics {
  total_agent_order_count: number;      // 总代理订单数
  today_agent_order_count: number;      // 今日代理订单数
}

// 代理统计数据
export interface AgentStatistics {
  total_agent_count: number;        // 总代理数
  today_agent_count: number;        // 今日新增代理数
}

// 代理链接产品统计项
export interface AgentLinkProductStatisticsItem {
  product_name: string;
  link_count: number;
}

// 代理链接产品统计响应
export interface AgentLinkProductStatisticsResp {
  items: AgentLinkProductStatisticsItem[];
}

// 代理链接产品统计请求参数
export interface GetAgentLinkProductStatisticsParams {}

  // 代理上级抽佣相关接口
  export interface AgentCommissionDeductionListItem {
    id: number;
    agent_id: number;
    deducted_agent_id: number;
    amount: number;
    product_name: string;
    type: 'cost' | 'pricing';
    status: number;
    create_time: string;
  }

  export interface AgentCommissionDeductionList {
    total: number;
    items: AgentCommissionDeductionListItem[];
  }

  export interface GetAgentCommissionDeductionListParams {
    page: number;
    pageSize: number;
    agent_id?: number;
    product_name?: string;
    type?: 'cost' | 'pricing';
    status?: number;
  }

  // 平台抽佣列表项
  export interface AgentPlatformDeductionListItem {
    id: number;
    agent_id: number;
    amount: number;
    type: 'cost' | 'pricing';
    status: number;
    create_time: string;
  }

  // 平台抽佣列表响应
  export interface AgentPlatformDeductionList {
    total: number;
    items: AgentPlatformDeductionListItem[];
  }

  // 获取平台抽佣列表参数
  export interface GetAgentPlatformDeductionListParams {
    page: number;
    pageSize: number;
    agent_id?: number;
    type?: 'cost' | 'pricing';
    status?: number;
  }

  // 代理产品配置列表项
  export interface AgentProductionConfigItem {
    id: number;
    product_name: string;
    cost_price: number;
    price_range_min: number;
    price_range_max: number;
    pricing_standard: number;
    overpricing_ratio: number;
    create_time: string;
  }

  // 代理产品配置列表响应
  export interface AgentProductionConfigList {
    total: number;
    items: AgentProductionConfigItem[];
  }

  // 获取代理产品配置列表参数
  export interface GetAgentProductionConfigListParams {
    page: number;
    pageSize: number;
    product_name?: string;
    id?: number;
  }

  // 更新代理产品配置参数
  export interface UpdateAgentProductionConfigParams {
    id: number;
    cost_price: number;
    price_range_min: number;
    price_range_max: number;
    pricing_standard: number;
    overpricing_ratio: number;
  }

  // 更新代理产品配置响应
  export interface UpdateAgentProductionConfigResp {
    success: boolean;
  }

  export interface MembershipRechargeOrderListItem {
    id: number;
    user_id: number;
    agent_id: number;
    level_name: string;
    amount: number;
    payment_method: 'alipay' | 'appleiap' | 'other' | 'wechat';
    order_no: string;
    platform_order_id: string;
    status: 'cancelled' | 'failed' | 'pending' | 'success';
    create_time: string;
  }

  export interface GetMembershipRechargeOrderListParams {
    page: number;
    pageSize: number;
    user_id?: number;
    agent_id?: number;
    level_name?: string;
    status?: string;
  }

  export interface MembershipRechargeOrderList {
    total: number;
    items: MembershipRechargeOrderListItem[];
  }

  // 代理会员配置相关接口
  export interface AgentMembershipConfigListItem {
    id: number;
    level_name: string;
    price: number;
    report_commission: number;
    lower_activity_reward: null | number;
    new_activity_reward: null | number;
    lower_standard_count: null | number;
    new_lower_standard_count: null | number;
    lower_withdraw_reward_ratio: null | number;
    lower_convert_vip_reward: null | number;
    lower_convert_svip_reward: null | number;
    exemption_amount: number;
    price_increase_max: null | number;
    price_ratio: null | number;
    price_increase_amount: null | number;
    create_time: string;
  }

  export interface GetAgentMembershipConfigListParams {
    page: number;
    pageSize: number;
    level_name?: string;
  }

  // 代理会员配置编辑请求参数
  export interface UpdateAgentMembershipConfigParams {
    id: number; // 主键
    level_name: string; // 会员级别名称
    price: number; // 会员年费
    report_commission: number; // 直推报告收益
    lower_activity_reward?: null | number; // 下级活跃奖励金额
    new_activity_reward?: null | number; // 新增活跃奖励金额
    lower_standard_count?: null | number; // 活跃下级达标个数
    new_lower_standard_count?: null | number; // 新增活跃下级达标个数
    lower_withdraw_reward_ratio?: null | number; // 下级提现奖励比例
    lower_convert_vip_reward?: null | number; // 下级转化VIP奖励
    lower_convert_svip_reward?: null | number; // 下级转化SVIP奖励
    exemption_amount?: null | number; // 免责金额
    price_increase_max?: null | number; // 提价最高金额
    price_ratio?: null | number; // 提价区间收取比例
    price_increase_amount?: null | number; // 在原本成本上加价的金额
  }

  // 代理钱包信息
  export interface AgentWalletInfo {
    balance: number; // 可用余额
    frozen_balance: number; // 冻结余额
    total_earnings: number; // 总收益
  }

  // 修改代理钱包余额请求
  export interface UpdateAgentWalletBalanceReq {
    agent_id: number; // 代理ID
    amount: number; // 修改金额（正数增加，负数减少）
  }

  // 修改代理钱包余额响应
  export interface UpdateAgentWalletBalanceResp {
    success: boolean; // 是否成功
    balance: number; // 修改后的余额
  }

  // 代理钱包流水相关接口
  export interface WalletTransactionListItem {
    id: number;
    agent_id: number;
    transaction_type: string;
    amount: number;
    balance_before: number;
    balance_after: number;
    frozen_balance_before: number;
    frozen_balance_after: number;
    transaction_id?: string;
    related_user_id?: number;
    remark?: string;
    create_time: string;
  }

  export interface WalletTransactionList {
    total: number;
    items: WalletTransactionListItem[];
  }

  export interface GetWalletTransactionListParams {
    page: number;
    pageSize: number;
    agent_id: number;
    transaction_type?: string;
    create_time_start?: string;
    create_time_end?: string;
  }

  // 系统配置相关接口
  export interface SystemConfig {
    commission_safe_mode: boolean; // 佣金安全防御模式
  }

  export interface UpdateSystemConfigReq {
    commission_safe_mode: boolean; // 佣金安全防御模式：true-冻结模式，false-直接结算模式
  }

  export interface UpdateSystemConfigResp {
    success: boolean; // 是否成功
  }

}

/**
 * 获取代理列表数据
 * @param params 查询参数
 */
async function getAgentList(params: AgentApi.GetAgentListParams) {
  return requestClient.get<AgentApi.AgentList>('/agent/list', {
    params,
  });
}

/**
 * 获取代理推广链接列表
 */
async function getAgentLinkList(params: AgentApi.GetAgentLinkListParams) {
  return requestClient.get<AgentApi.AgentLinkList>('/agent/agent-link/list', {
    params,
  });
}

/**
 * 获取代理佣金列表
 */
async function getAgentCommissionList(
  params: AgentApi.GetAgentCommissionListParams,
) {
  return requestClient.get<AgentApi.AgentCommissionList>(
    '/agent/agent-commission/list',
    {
      params,
    },
  );
}

/**
 * 更新代理佣金状态
 */
async function updateAgentCommissionStatus(
  id: number,
  status: number,
) {
  return requestClient.post<{ success: boolean }>(
    '/agent/agent-commission/update-status',
    {
      id,
      status,
    },
  );
}

/**
 * 批量解冻代理佣金
 */
async function batchUnfreezeAgentCommission(
  agentId?: number,
) {
  return requestClient.post<{
    success: boolean;
    count: number;
    amount: number;
  }>('/agent/agent-commission/batch-unfreeze', {
    agent_id: agentId,
  });
}

/**
 * 获取代理奖励列表
 */
async function getAgentRewardList(params: AgentApi.GetAgentRewardListParams) {
  return requestClient.get<AgentApi.AgentRewardList>(
    '/agent/agent-reward/list',
    {
      params,
    },
  );
}

/**
 * 获取代理提现列表
 */
async function getAgentWithdrawalList(
  params: AgentApi.GetAgentWithdrawalListParams,
) {
  return requestClient.get<AgentApi.AgentWithdrawalList>(
    '/agent/agent-withdrawal/list',
    {
      params,
    },
  );
}

/**
 * 获取代理上级抽佣列表
 */
async function getAgentCommissionDeductionList(
  params: AgentApi.GetAgentCommissionDeductionListParams,
) {
  return requestClient.get<AgentApi.AgentCommissionDeductionList>(
    '/agent/agent-commission-deduction/list',
    {
      params,
    },
  );
}

/**
 * 获取平台抽佣列表
 */
async function getAgentPlatformDeductionList(
  params: AgentApi.GetAgentPlatformDeductionListParams,
) {
  return requestClient.get<AgentApi.AgentPlatformDeductionList>(
    '/agent/agent-platform-deduction/list',
    {
      params,
    },
  );
}

/**
 * 获取代理产品配置列表
 */
async function getAgentProductionConfigList(
  params: AgentApi.GetAgentProductionConfigListParams,
) {
  return requestClient.get<AgentApi.AgentProductionConfigList>(
    '/agent/agent-production-config/list',
    {
      params,
    },
  );
}

/**
 * 更新代理产品配置
 */
async function updateAgentProductionConfig(
  params: AgentApi.UpdateAgentProductionConfigParams,
) {
  return requestClient.post<AgentApi.UpdateAgentProductionConfigResp>(
    '/agent/agent-production-config/update',
    params,
  );
}

/**
 * 获取会员充值订单列表
 */
async function getMembershipRechargeOrderList(
  params: AgentApi.GetMembershipRechargeOrderListParams,
) {
  return requestClient.get<AgentApi.MembershipRechargeOrderList>(
    '/agent/agent-membership-recharge-order/list',
    {
      params,
    },
  );
}

/**
 * 获取代理会员配置列表
 */
async function getAgentMembershipConfigList(
  params: AgentApi.GetAgentMembershipConfigListParams,
) {
  return requestClient.get<{
    items: AgentApi.AgentMembershipConfigListItem[];
    total: number;
  }>('/agent/agent-membership-config/list', { params });
}

/**
 * 更新代理会员配置
 */
async function updateAgentMembershipConfig(
  params: AgentApi.UpdateAgentMembershipConfigParams,
) {
  return requestClient.post<{ success: boolean }>(
    '/agent/agent-membership-config/update',
    params,
  );
}

/**
 * 银行卡提现审核
 */
export interface ReviewBankCardWithdrawalParams {
  withdrawal_id: number;
  action: 1 | 2; // 1-确认, 2-拒绝
  remark?: string;
}

async function reviewBankCardWithdrawal(
  params: ReviewBankCardWithdrawalParams,
) {
  return requestClient.post<{ success: boolean }>(
    '/agent/agent-withdrawal/bank-card/review',
    params,
  );
}

/**
 * 获取提现统计数据
 */
async function getWithdrawalStatistics() {
  return requestClient.get<AgentApi.WithdrawalStatistics>(
    '/agent/agent-withdrawal/statistics',
  );
}

/**
 * 获取代理订单统计数据
 */
async function getAgentOrderStatistics() {
  return requestClient.get<AgentApi.AgentOrderStatistics>(
    '/agent/agent-order/statistics',
  );
}

/**
 * 获取代理统计数据
 */
async function getAgentStatistics() {
  return requestClient.get<AgentApi.AgentStatistics>(
    '/agent/statistics',
  );
}

/**
 * 获取代理链接产品统计数据
 */
async function getAgentLinkProductStatistics() {
  return requestClient.get<AgentApi.AgentLinkProductStatisticsResp>(
    '/agent/agent-link/product-statistics',
  );
}

/**
 * 获取代理钱包信息
 */
async function getAgentWallet(agentId: number) {
  return requestClient.get<AgentApi.AgentWalletInfo>(
    `/agent/wallet/${agentId}`,
  );
}

/**
 * 修改代理钱包余额
 */
async function updateAgentWalletBalance(params: AgentApi.UpdateAgentWalletBalanceReq) {
  return requestClient.post<AgentApi.UpdateAgentWalletBalanceResp>(
    '/agent/wallet/update-balance',
    params,
  );
}

/**
 * 获取系统配置
 */
async function getSystemConfig() {
  return requestClient.get<AgentApi.SystemConfig>(
    '/agent/system-config',
  );
}

/**
 * 更新系统配置
 */
async function updateSystemConfig(params: AgentApi.UpdateSystemConfigReq) {
  return requestClient.post<AgentApi.UpdateSystemConfigResp>(
    '/agent/system-config',
    params,
  );
}

/**
 * 获取代理钱包流水列表
 */
async function getWalletTransactionList(
  params: AgentApi.GetWalletTransactionListParams,
) {
  return requestClient.get<AgentApi.WalletTransactionList>(
    '/agent/wallet-transaction/list',
    { params },
  );
}



export {
  batchUnfreezeAgentCommission,
  getAgentCommissionDeductionList,
  getAgentCommissionList,
  getAgentLinkList,
  getAgentLinkProductStatistics,
  getAgentList,
  getAgentMembershipConfigList,
  getAgentOrderStatistics,
  getAgentPlatformDeductionList,
  getAgentProductionConfigList,
  getAgentRewardList,
  getAgentStatistics,
  getAgentWallet,
  getAgentWithdrawalList,
  getMembershipRechargeOrderList,
  getWithdrawalStatistics,
  reviewBankCardWithdrawal,
  updateAgentCommissionStatus,
  updateAgentMembershipConfig,
  updateAgentProductionConfig,
  updateAgentWalletBalance,
  getSystemConfig,
  updateSystemConfig,
  getWalletTransactionList,
};
