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

export namespace AgentApi {
  export interface AgentListItem {
    id: number;
    user_id: number;
    agent_code: number;
    level: number; // 1=普通，2=黄金，3=钻石
    level_name: string;
    region: string;
    mobile: string;
    wechat_id?: string;
    team_leader_id?: number;
    balance: number;
    total_earnings: number;
    frozen_balance: number;
    withdrawn_amount: number;
    is_real_name: boolean;
    create_time: string;
  }

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

  export interface GetAgentListParams {
    page: number;
    pageSize: number;
    mobile?: string;
    region?: string;
    level?: number;
    team_leader_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_id: number;
    product_name: string;
    set_price: number;
    actual_base_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_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;
    product_name?: string;
    status?: number;
  }

  // 代理返佣相关接口
  export interface AgentRebateListItem {
    id: number;
    agent_id: number;
    source_agent_id: number;
    order_id: number;
    rebate_type: number; // 1=直接上级返佣，2=钻石上级返佣，3=黄金上级返佣
    amount: number;
    create_time: string;
  }

  export interface AgentRebateList {
    total: number;
    items: AgentRebateListItem[];
  }

  export interface GetAgentRebateListParams {
    page: number;
    pageSize: number;
    agent_id?: number;
    source_agent_id?: number;
    rebate_type?: number;
  }

  // 代理提现相关接口
  export interface AgentWithdrawalListItem {
    id: number;
    agent_id: number;
    withdraw_no: string;
    amount: number;
    tax_amount: number;
    actual_amount: number;
    status: number; // 1=待审核，2=审核通过，3=审核拒绝，4=提现中，5=提现成功，6=提现失败
    payee_account: string;
    payee_name: string;
    remark: string;
    create_time: 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 AuditWithdrawalParams {
    withdrawal_id: number;
    status: number; // 2=通过，3=拒绝
    remark: string;
  }

  // 代理产品配置列表项
  export interface AgentProductionConfigItem {
    id: number;
    product_id: number;
    product_name: string;
    base_price: number;
    price_range_min: number;
    price_range_max: number;
    price_threshold: number;
    price_fee_rate: number;
    create_time: string;
  }

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

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

  // 更新代理产品配置参数
  export interface UpdateAgentProductionConfigParams {
    id: number;
    base_price: number;
    price_range_max: number;
    price_threshold?: number;
    price_fee_rate?: number;
  }

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

  // 代理升级记录相关接口
  export interface AgentUpgradeListItem {
    id: number;
    agent_id: number;
    from_level: number;
    to_level: number;
    upgrade_type: number; // 1=自主付费，2=钻石升级下级
    upgrade_fee: number;
    rebate_amount: number;
    status: number; // 1=待处理，2=已完成，3=已失败
    create_time: string;
  }

  export interface AgentUpgradeList {
    total: number;
    items: AgentUpgradeListItem[];
  }

  export interface GetAgentUpgradeListParams {
    page: number;
    pageSize: number;
    agent_id?: number;
    upgrade_type?: number;
    status?: number;
  }

  // 代理订单相关接口
  export interface AgentOrderListItem {
    id: number;
    agent_id: number;
    order_id: number;
    product_id: number;
    product_name: string;
    order_amount: number;
    set_price: number;
    actual_base_price: number;
    price_cost: number;
    agent_profit: number;
    process_status: number; // 0=待处理，1=处理成功，2=处理失败
    create_time: string;
  }

  export interface AgentOrderList {
    total: number;
    items: AgentOrderListItem[];
  }

  export interface GetAgentOrderListParams {
    page: number;
    pageSize: number;
    agent_id?: number;
    order_id?: number;
    process_status?: number;
  }

  // 邀请码管理相关接口
  export interface InviteCodeListItem {
    id: number;
    code: string;
    agent_id: number; // 0表示平台发放
    agent_mobile: string;
    target_level: number;
    status: number; // 0=未使用，1=已使用，2=已失效
    used_user_id?: number;
    used_agent_id?: number;
    used_time?: string;
    expire_time?: string;
    remark?: string;
    create_time: string;
  }

  export interface InviteCodeList {
    total: number;
    items: InviteCodeListItem[];
  }

  export interface GetInviteCodeListParams {
    page: number;
    pageSize: number;
    code?: string;
    agent_id?: number;
    target_level?: number;
    status?: number;
  }

  export interface GenerateDiamondInviteCodeParams {
    count: number;
    expire_days?: number; // 可选，0表示不过期
    remark?: string;
  }

  export interface GenerateDiamondInviteCodeResp {
    codes: string[];
  }

  // 系统配置相关接口（价格配置已移除，改为产品配置表管理）
  export interface AgentConfig {
    level_bonus: {
      diamond: number;
      gold: number;
      normal: number;
    };
    upgrade_fee: {
      normal_to_gold: number;
      normal_to_diamond: number;
      gold_to_diamond: number;
    };
    upgrade_rebate: {
      normal_to_gold_rebate: number;
      to_diamond_rebate: number;
    };
    direct_parent_rebate: {
      diamond: number; // 直接上级是钻石的返佣金额（6元）
      gold: number; // 直接上级是黄金的返佣金额（3元）
      normal: number; // 直接上级是普通的返佣金额（2元）
    };
    max_gold_rebate_amount: number; // 黄金代理最大返佣金额（3元）
    commission_freeze: {
      ratio: number; // 佣金冻结比例（例如：0.1表示10%）
      threshold: number; // 佣金冻结阈值（订单单价达到此金额才触发冻结，单位：元）
      days: number; // 佣金冻结解冻天数（单位：天，例如：30表示30天后解冻）
    };
    tax_rate: number;
    tax_exemption_amount: number;
    gold_max_uplift_amount: number;
    diamond_max_uplift_amount: number;
  }

  export interface UpdateAgentConfigParams {
    level_bonus?: {
      diamond?: number;
      gold?: number;
      normal?: number;
    };
    upgrade_fee?: {
      normal_to_gold?: number;
      normal_to_diamond?: number;
      gold_to_diamond?: number;
    };
    upgrade_rebate?: {
      normal_to_gold_rebate?: number;
      to_diamond_rebate?: number;
    };
    direct_parent_rebate?: {
      diamond?: number; // 直接上级是钻石的返佣金额（6元）
      gold?: number; // 直接上级是黄金的返佣金额（3元）
      normal?: number; // 直接上级是普通的返佣金额（2元）
    };
    max_gold_rebate_amount?: number; // 黄金代理最大返佣金额（3元）
    commission_freeze?: {
      ratio?: number; // 佣金冻结比例（例如：0.1表示10%）
      threshold?: number; // 佣金冻结阈值（订单单价达到此金额才触发冻结，单位：元）
      days?: number; // 佣金冻结解冻天数（单位：天，例如：30表示30天后解冻）
    };
    tax_rate?: number;
    tax_exemption_amount?: number;
    gold_max_uplift_amount?: number;
    diamond_max_uplift_amount?: number;
  }

  // 实名认证相关接口
  export interface AgentRealNameListItem {
    id: number;
    agent_id: number;
    name: string;
    id_card: string; // 加密，需要脱敏显示
    mobile: string; // 加密
    status: number; // 1=未验证，2=已通过
    verify_time?: string;
    create_time: string;
  }

  export interface AgentRealNameList {
    total: number;
    items: AgentRealNameListItem[];
  }

  export interface GetAgentRealNameListParams {
    page: number;
    pageSize: number;
    agent_id?: number;
    status?: number;
  }

  // 代理奖励相关接口
  export interface AgentRewardListItem {
    id: number;
    agent_id: number;
    type: string; // 奖励类型：register=注册奖励，first_order=首单奖励，level_up=升级奖励
    amount: number;
    order_id?: number;
    status: string; // pending=待发放，paid=已发放，failed=发放失败
    create_time: string;
    pay_time?: string;
  }

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

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

/**
 * 获取代理列表数据
 * @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/link/list', {
    params,
  });
}

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

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

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

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

/**
 * 获取代理返佣记录列表
 */
async function getAgentRebateList(params: AgentApi.GetAgentRebateListParams) {
  return requestClient.get<AgentApi.AgentRebateList>('/agent/rebate/list', {
    params,
  });
}

/**
 * 获取代理升级记录列表
 */
async function getAgentUpgradeList(
  params: AgentApi.GetAgentUpgradeListParams,
) {
  return requestClient.get<AgentApi.AgentUpgradeList>('/agent/upgrade/list', {
    params,
  });
}

/**
 * 获取代理订单列表
 */
async function getAgentOrderList(params: AgentApi.GetAgentOrderListParams) {
  return requestClient.get<AgentApi.AgentOrderList>('/agent/order/list', {
    params,
  });
}

/**
 * 获取邀请码列表
 */
async function getInviteCodeList(params: AgentApi.GetInviteCodeListParams) {
  return requestClient.get<AgentApi.InviteCodeList>(
    '/agent/invite_code/list',
    {
      params,
    },
  );
}

/**
 * 生成钻石邀请码
 */
async function generateDiamondInviteCode(
  params: AgentApi.GenerateDiamondInviteCodeParams,
) {
  return requestClient.post<AgentApi.GenerateDiamondInviteCodeResp>(
    '/agent/invite_code/diamond/generate',
    params,
  );
}

/**
 * 获取系统配置
 */
async function getAgentConfig() {
  return requestClient.get<AgentApi.AgentConfig>('/agent/config');
}

/**
 * 更新系统配置
 */
async function updateAgentConfig(params: AgentApi.UpdateAgentConfigParams) {
  return requestClient.post<{ success: boolean }>(
    '/agent/config/update',
    params,
  );
}

/**
 * 获取实名认证列表
 */
async function getAgentRealNameList(
  params: AgentApi.GetAgentRealNameListParams,
) {
  return requestClient.get<AgentApi.AgentRealNameList>(
    '/agent/real_name/list',
    {
      params,
    },
  );
}

/**
 * 审核提现
 */
async function auditWithdrawal(params: AgentApi.AuditWithdrawalParams) {
  return requestClient.post<{ success: boolean }>(
    '/agent/withdrawal/audit',
    params,
  );
}

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

export {
  auditWithdrawal,
  generateDiamondInviteCode,
  getAgentCommissionList,
  getAgentConfig,
  getAgentLinkList,
  getAgentList,
  getAgentOrderList,
  getAgentProductionConfigList,
  getAgentRebateList,
  getAgentRealNameList,
  getAgentRewardList,
  getAgentUpgradeList,
  getAgentWithdrawalList,
  getInviteCodeList,
  updateAgentConfig,
  updateAgentProductionConfig,
};
