import useApiFetch from '@/composables/useApiFetch'

function buildQueryString(params) {
  const parts = []
  Object.keys(params || {}).forEach((key) => {
    const v = params[key]
    if (v !== undefined && v !== null && v !== '')
      parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(v))}`)
  })
  const s = parts.join('&')
  return s ? `?${s}` : ''
}

export function getAgentInfo() {
  return useApiFetch('/agent/info').get().json()
}

export function getTeamStatistics() {
  return useApiFetch('/agent/team/statistics').get().json()
}

export function getConversionRate() {
  return useApiFetch('/agent/conversion/rate').get().json()
}

export function getRevenueInfo() {
  return useApiFetch('/agent/revenue').get().json()
}

/**
 * 我的推广收益明细（佣金列表）
 * @param {object} params - { page, page_size }
 */
export function getCommissionList(params) {
  return useApiFetch(`/agent/commission/list${buildQueryString(params)}`).get().json()
}

/**
 * 下级推广收益明细（返佣列表）
 * @param {object} params - { page, page_size, rebate_type? }
 */
export function getRebateList(params) {
  return useApiFetch(`/agent/rebate/list${buildQueryString(params)}`).get().json()
}

export function getProductConfig() {
  return useApiFetch('/agent/product_config').get().json()
}

/**
 * 获取推广链接数据
 * @param {string} linkIdentifier - 推广链接标识
 */
export function getLinkData(linkIdentifier) {
  return useApiFetch(
    `/agent/link?link_identifier=${encodeURIComponent(linkIdentifier)}`,
  )
    .get()
    .json()
}

/**
 * 生成推广链接
 * @param {object} params - 生成参数
 * @param {number} params.product_id - 产品ID
 * @param {number} params.set_price - 设定价格
 * @param {string} [params.target_path] - 目标路径模板（如 /agent/promotionInquire/）
 */
export function generateLink(params) {
  return useApiFetch('/agent/generating_link').post(params).json()
}

export function getTeamList(params) {
  return useApiFetch(`/agent/team/list${buildQueryString(params)}`).get().json()
}

export function getSubordinateList(params) {
  return useApiFetch(`/agent/subordinate/list${buildQueryString(params)}`).get().json()
}

export function applyForAgent(params) {
  return useApiFetch('/agent/apply').post(params).json()
}

export function registerByInviteCode(params) {
  return useApiFetch('/agent/register/invite').post(params).json()
}

export function applyWithdrawal(params) {
  return useApiFetch('/agent/withdrawal/apply').post(params).json()
}

export function getWithdrawalList(params) {
  return useApiFetch(`/agent/withdrawal/list${buildQueryString(params)}`).get().json()
}

export function realNameAuth(params) {
  return useApiFetch('/agent/real_name').post(params).json()
}

export function generateInviteCode(params) {
  return useApiFetch('/agent/invite_code/generate').post(params).json()
}

export function getInviteCodeList(params) {
  return useApiFetch(`/agent/invite_code/list${buildQueryString(params)}`).get().json()
}

/**
 * 删除邀请码
 * @param {object} params - { id: 邀请码ID }
 */
export function deleteInviteCode(params) {
  return useApiFetch('/agent/invite_code/delete').post(params).json()
}

/**
 * 获取邀请链接（短链）
 * @param {object} params - { invite_code: 邀请码, target_path?: 目标路径 }
 */
export function getInviteLink(params) {
  const queryString = buildQueryString(params || {})
  return useApiFetch(`/agent/invite_link${queryString}`).get().json()
}

/**
 * 生成邀请海报（带二维码的图片）
 * @param {object} params - { invite_link: 邀请链接 }
 */
export function generateInvitePoster(params) {
  const queryString = buildQueryString(params || {})
  return useApiFetch(`/agent/invite/poster${queryString}`).get().json()
}

/**
 * 获取等级权益与升级费用
 */
export function getLevelPrivilege() {
  return useApiFetch('/agent/level/privilege').get().json()
}

export function applyUpgrade(params) {
  return useApiFetch('/agent/upgrade/apply').post(params).json()
}

export function upgradeSubordinate(params) {
  return useApiFetch('/agent/upgrade/subordinate').post(params).json()
}
