/**
 * CRM 线索管理模块 API 调用
 * 服务归属：hzhub-system (端口 8083)
 * API前缀：/crm/lead（通过 Gateway 路由）
 */

import type {
  CrmLeadBo,
  CrmLeadFollowBo,
  CrmLeadFollowVo,
  CrmLeadVo,
  CrmOpportunityBo,
  CrmOpportunityVo,
  LeadAssignRequest,
  LeadConvertRequest,
  LeadQueryParams,
  OpportunityQueryParams,
  R,
  TableDataInfo,
} from './types';

// 导出类型供外部使用
export type {
  CrmLeadBo,
  CrmLeadFollowBo,
  CrmLeadFollowVo,
  CrmLeadVo,
  CrmOpportunityBo,
  CrmOpportunityVo,
  LeadAssignRequest,
  LeadConvertRequest,
  LeadQueryParams,
  OpportunityQueryParams,
  R,
  TableDataInfo,
} from './types';
import request from '@/utils/request';

/**
 * 获取线索列表（分页）
 */
export function getLeadList(params: LeadQueryParams): Promise<TableDataInfo<CrmLeadVo>> {
  return request.get('/crm/lead/list', params).json();
}

/**
 * 获取线索详情
 */
export function getLeadDetail(leadId: number): Promise<R<CrmLeadVo>> {
  return request.get(`/crm/lead/${leadId}`).json();
}

/**
 * 新增线索
 */
export function createLead(data: CrmLeadBo): Promise<R<void>> {
  return request.post('/crm/lead', data).json();
}

/**
 * 编辑线索
 */
export function updateLead(data: CrmLeadBo): Promise<R<void>> {
  return request.put('/crm/lead', data).json();
}

/**
 * 删除线索（支持批量）
 */
export function deleteLead(leadIds: string): Promise<R<void>> {
  return request.delete(`/crm/lead/${leadIds}`).json();
}

/**
 * 分配线索
 */
export function assignLead(data: LeadAssignRequest): Promise<R<void>> {
  return request.put('/crm/lead/assign', data).json();
}

/**
 * 获取线索跟进记录列表
 */
export function getLeadFollowRecords(leadId: number): Promise<R<CrmLeadFollowVo[]>> {
  return request.get(`/crm/lead/follow/${leadId}`).json();
}

/**
 * 添加线索跟进记录
 */
export function addLeadFollow(data: CrmLeadFollowBo): Promise<R<void>> {
  return request.post('/crm/lead/follow', data).json();
}

/**
 * 线索转经销商（第二阶段实现）
 */
export function convertLeadToDealer(data: LeadConvertRequest): Promise<R<void>> {
  return request.post('/crm/lead/convert', data).json();
}

/**
 * ========================================
 * CRM 商机管理模块 API 调用
 * ========================================
 */

/**
 * 获取商机列表（分页）
 */
export function getOpportunityList(params: OpportunityQueryParams): Promise<TableDataInfo<CrmOpportunityVo>> {
  return request.get('/crm/opportunity/list', params).json();
}

/**
 * 获取商机详情
 */
export function getOpportunityDetail(opportunityId: number): Promise<R<CrmOpportunityVo>> {
  return request.get(`/crm/opportunity/${opportunityId}`).json();
}

/**
 * 新增商机
 */
export function createOpportunity(data: CrmOpportunityBo): Promise<R<void>> {
  return request.post('/crm/opportunity', data).json();
}

/**
 * 编辑商机
 */
export function updateOpportunity(data: CrmOpportunityBo): Promise<R<void>> {
  return request.put('/crm/opportunity', data).json();
}

/**
 * 删除商机（支持批量）
 */
export function deleteOpportunity(opportunityIds: string): Promise<R<void>> {
  return request.delete(`/crm/opportunity/${opportunityIds}`).json();
}
