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

import type {
  CrmDealerVo,
  CrmLeadBo,
  CrmLeadFollowBo,
  CrmLeadFollowVo,
  CrmLeadStatsVo,
  CrmLeadVo,
  CrmOpportunityBo,
  CrmOpportunityVo,
  CrmSyncAlertVo,
  DealerQueryParams,
  ErpCustomerSelectVo,
  InstantSyncResult,
  LeadAssignRequest,
  LeadConvertRequest,
  LeadQueryParams,
  OpportunityQueryParams,
  R,
  TableDataInfo,
} from './types';

// 导出类型供外部使用
export type {
  CrmDealerVo,
  CrmLeadBo,
  CrmLeadFollowBo,
  CrmLeadFollowVo,
  CrmLeadStatsVo,
  CrmLeadVo,
  CrmOpportunityBo,
  CrmOpportunityVo,
  CrmSyncAlertVo,
  DealerQueryParams,
  ErpCustomerSelectVo,
  InstantSyncResult,
  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 getLeadStats(): Promise<R<CrmLeadStatsVo>> {
  return request.get('/crm/lead/stats').json();
}

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

/**
 * 作废线索
 */
export function invalidateLead(leadId: number): Promise<R<void>> {
  return request.put(`/crm/lead/invalidate/${leadId}`).json();
}

/**
 * 恢复线索
 */
export function restoreLead(leadId: number): Promise<R<void>> {
  return request.put(`/crm/lead/restore/${leadId}`).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();
}

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

/**
 * 获取经销商选择器列表
 * 用于商机创建、商机分配等场景
 */
export function getDealerSelectList(keyword?: string): Promise<R<CrmDealerVo[]>> {
  return request.get('/crm/dealer/portal/select', { keyword }).json();
}

/**
 * ========================================
 * CRM 经销商管理扩展 API
 * ========================================
 */

/**
 * 获取CRM经销商分页列表
 */
export function getDealerList(params: DealerQueryParams): Promise<TableDataInfo<CrmDealerVo>> {
  return request.get('/crm/dealer/portal/list', params).json();
}

/**
 * 获取经销商详情（含同步状态）
 */
export function getDealerDetail(dealerId: number): Promise<R<CrmDealerVo>> {
  return request.get(`/crm/dealer/portal/detail/${dealerId}`).json();
}

/**
 * ERP客户选择器数据（用于线索转化/经销商创建）
 */
export function getErpCustomerSelect(keyword?: string): Promise<R<ErpCustomerSelectVo[]>> {
  return request.get('/crm/dealer/portal/erp-select', { keyword }).json();
}

/**
 * 校验ERP客户编码是否存在
 */
export function validateCustomerCode(customerCode: string): Promise<R<boolean>> {
  return request.get('/crm/dealer/portal/validate-code', { customerCode }).json();
}

/**
 * 手动同步经销商
 */
export function manualSyncDealer(dealerId: number): Promise<R<InstantSyncResult>> {
  return request.post(`/crm/dealer/portal/sync/${dealerId}`).json();
}

/**
 * 获取经销商的待处理预警列表
 */
export function getDealerAlerts(dealerId: number): Promise<R<CrmSyncAlertVo[]>> {
  return request.get(`/crm/dealer/portal/alerts/${dealerId}`).json();
}

/**
 * 处理预警
 */
export function resolveAlert(alertId: number, action: string, note?: string): Promise<R<void>> {
  return request.put(`/crm/dealer/portal/alerts/${alertId}`, null, { params: { action, note } }).json();
}
