import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import {
  CustomerLead,
  CustomerLeadParam,
  LeadDispatchParam,
  LeadFollowParam,
  LeadDispatch,
  LeadFollowLog,
  LeadStatistics,
  SalesmanStats
} from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询客资列表
 */
export async function pageCustomerLead(params: CustomerLeadParam) {
  const res = await request.get<ApiResult<PageResult<CustomerLead>>>(
    MODULES_API_URL + '/customer/lead/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取客资详情
 */
export async function getCustomerLeadDetail(leadId: number) {
  const res = await request.get<ApiResult<CustomerLead>>(
    MODULES_API_URL + '/customer/lead/detail/' + leadId
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 创建客资（管理员录入）
 */
export async function createCustomerLead(data: CustomerLead) {
  const res = await request.post<ApiResult<CustomerLead>>(
    MODULES_API_URL + '/customer/lead/create',
    data
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 更新客资信息
 */
export async function updateCustomerLead(data: CustomerLead) {
  const res = await request.put<ApiResult<boolean>>(
    MODULES_API_URL + '/customer/lead/update',
    data
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 更新客资状态
 */
export async function updateCustomerLeadStatus(
  leadId: number,
  status: number,
  remarks?: string
) {
  const res = await request.put<ApiResult<boolean>>(
    MODULES_API_URL + '/customer/lead/status/' + leadId,
    { status, remarks }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 派单给业务员
 */
export async function dispatchLead(data: LeadDispatchParam) {
  const res = await request.post<ApiResult<boolean>>(
    MODULES_API_URL + '/customer/lead/dispatch',
    data
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量派单
 */
export async function batchDispatchLeads(data: LeadDispatchParam) {
  const res = await request.post<ApiResult<{ successCount: number; failCount: number; errors: string[] }>>(
    MODULES_API_URL + '/customer/lead/dispatch/batch',
    data
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加跟进记录
 */
export async function addLeadFollowLog(data: LeadFollowParam) {
  const res = await request.post<ApiResult<boolean>>(
    MODULES_API_URL + '/customer/lead/follow',
    data
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取跟进历史
 */
export async function getLeadFollowHistory(leadId: number) {
  const res = await request.get<ApiResult<LeadFollowLog[]>>(
    MODULES_API_URL + '/customer/lead/follow/history/' + leadId
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取统计数据
 */
export async function getLeadStatistics(params: CustomerLeadParam) {
  const res = await request.get<ApiResult<LeadStatistics>>(
    MODULES_API_URL + '/customer/lead/statistics',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导出客资数据
 */
export async function exportCustomerLeads(params: CustomerLeadParam) {
  const res = await request.get<ApiResult<Record<string, unknown>[]>>(
    MODULES_API_URL + '/customer/lead/export',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取未分配客资列表
 */
export async function getUnassignedLeads() {
  const res = await request.get<ApiResult<CustomerLead[]>>(
    MODULES_API_URL + '/customer/lead/unassigned'
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取业务员客资统计
 */
export async function getSalesmanLeadStats(salesmanId: number) {
  const res = await request.get<ApiResult<SalesmanStats>>(
    MODULES_API_URL + '/customer/lead/salesman/stats/' + salesmanId
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取派单历史
 */
export async function getLeadDispatchHistory(leadId: number) {
  const res = await request.get<ApiResult<LeadDispatch[]>>(
    MODULES_API_URL + '/customer/lead/dispatch/history/' + leadId
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
