/**
 * 客资推荐人 API
 * 小程序端调用
 */
import request from '@/utils/request'

/**
 * 推荐人报备参数
 */
export interface ReferralParam {
  customerName: string
  customerPhone: string
  customerCompany?: string
  requirement?: string
  appointmentTime?: string
  remarks?: string
}

/**
 * 推荐人报备结果
 */
export interface ReferralResult {
  referralId: number
  referralCode: string
  customerName: string
  customerPhone: string
  referralFee?: number
  referralStatus: number
  referralStatusText: string
  createTime: string
}

/**
 * 推荐人统计
 */
export interface ReferralStats {
  totalCount: number
  pendingCount: number
  validCount: number
  settledCount: number
  pendingAmount: number
}

/**
 * 推荐人记录
 */
export interface ReferralRecord {
  referralId: number
  referredLeadId: number
  customerName: string
  customerPhone: string
  referralFee: number
  referralStatus: number
  referralStatusText: string
  leadStatus?: number
  leadStatusText?: string
  dealAmount?: number
  createTime: string
  settlementTime?: string
}

/**
 * 注册用户报备客户
 */
export function addReferral(data: ReferralParam) {
  return request.post<{ code: number; message: string; data: ReferralResult }>(
    '/lead/referral/user',
    data
  )
}

/**
 * 获取推荐人的推荐记录
 */
export function getReferralList(params: { pageNum?: number; pageSize?: number }) {
  return request.get<{ code: number; message: string; data: { list: ReferralRecord[]; total: number } }>(
    '/lead/referral/page',
    { params }
  )
}

/**
 * 获取推荐人统计
 */
export function getReferralStats(userId: number) {
  return request.get<{ code: number; message: string; data: ReferralStats }>(
    `/lead/referral/stats/${userId}`
  )
}

/**
 * 生成推荐码
 */
export function generateReferralCode() {
  return request.get<{ code: number; message: string; data: string }>(
    '/lead/referral/generateCode'
  )
}

/**
 * 获取当前用户的推荐码
 */
export function getMyReferralCode() {
  return request.get<{ code: number; message: string; data: { referralCode: string } }>(
    '/lead/referral/my/code'
  )
}

/**
 * 确认推荐有效（管理员）
 */
export function confirmReferral(referralId: number) {
  return request.put<{ code: number; message: string }>(
    `/lead/referral/confirm/${referralId}`
  )
}

/**
 * 作废推荐（管理员）
 */
export function invalidateReferral(referralId: number, reason?: string) {
  return request.put<{ code: number; message: string }>(
    `/lead/referral/invalidate/${referralId}`,
    { reason }
  )
}

/**
 * 结算推荐费（管理员）
 */
export function settleReferral(referralId: number) {
  return request.put<{ code: number; message: string }>(
    `/lead/referral/settle/${referralId}`
  )
}
