import request from '@/utils/request'

// ======== 工作流执行 (Instance) ========
export function listInstances(params) {
  return request({ url: '/workflow/lobster-exec/instance/list', method: 'get', params })
}

export function getInstance(instanceId) {
  return request({ url: `/workflow/lobster-exec/instance/${instanceId}`, method: 'get' })
}

export function getNodeLogs(instanceId) {
  return request({ url: `/workflow/lobster-exec/node-logs/${instanceId}`, method: 'get' })
}

export function startWorkflow(params) {
  return request({ url: '/workflow/lobster-exec/start', method: 'post', params })
}

export function executeNextNode(params) {
  return request({ url: '/workflow/lobster-exec/next-node', method: 'post', params })
}

export function pauseInstance(instanceId) {
  return request({ url: `/workflow/lobster-exec/pause/${instanceId}`, method: 'post' })
}

export function resumeInstance(instanceId) {
  return request({ url: `/workflow/lobster-exec/resume/${instanceId}`, method: 'post' })
}

export function terminateInstance(instanceId, reason) {
  return request({ url: `/workflow/lobster-exec/terminate/${instanceId}`, method: 'post', params: { reason } })
}

export function getControlMode(instanceId) {
  return request({ url: `/workflow/lobster-exec/control-mode/${instanceId}`, method: 'get' })
}

export function setControlMode(instanceId, mode) {
  return request({ url: `/workflow/lobster-exec/control-mode/${instanceId}`, method: 'post', params: { mode } })
}

export function completeHandoff(instanceId, nextNodeCode) {
  return request({ url: `/workflow/lobster-exec/complete-handoff/${instanceId}`, method: 'post', params: { nextNodeCode } })
}

// ======== 合规规则 ========
export function listComplianceRules() {
  return request({ url: '/workflow/lobster-exec/compliance-rules', method: 'get' })
}

export function addComplianceRule(data) {
  return request({ url: '/workflow/lobster-exec/compliance-rule', method: 'post', data })
}

export function updateComplianceRule(id, data) {
  return request({ url: `/workflow/lobster-exec/compliance-rule/${id}`, method: 'put', data })
}

export function deleteComplianceRule(id) {
  return request({ url: `/workflow/lobster-exec/compliance-rule/${id}`, method: 'delete' })
}

// ======== 引擎核心 ========
export function getEvolutionMetrics() {
  return request({ url: '/workflow/lobster/engine/evolution/metrics', method: 'get' })
}

export function analyzeEvolution(workflowId) {
  return request({ url: '/workflow/lobster/engine/evolution/analyze', method: 'get', params: { workflowId } })
}

export function applyEvolution(suggestionId) {
  return request({ url: '/workflow/lobster/engine/evolution/apply', method: 'post', params: { suggestionId } })
}

export function getHeartbeatStatus(instanceId) {
  return request({ url: '/workflow/lobster/engine/heartbeat/status', method: 'get', params: { instanceId } })
}

export function getAvailableChannels() {
  return request({ url: '/workflow/lobster/engine/channels', method: 'get' })
}

export function getNodeCapabilities() {
  return request({ url: '/workflow/lobster/engine/node-capabilities', method: 'get' })
}

export function runIntegrationTests() {
  return request({ url: '/workflow/lobster/engine/integration-test/run', method: 'post' })
}

export function testNodeTypes() {
  return request({ url: '/workflow/lobster/engine/integration-test/node-types', method: 'get' })
}

export function testDynamicExecutor() {
  return request({ url: '/workflow/lobster/engine/integration-test/dynamic-executor', method: 'get' })
}

// ======== AI生成工作流 ========
export function aiGenerate(data) {
  return request({ url: '/workflow/lobster/ai-generator/generate', method: 'post', data })
}

export function getGenerateResult(recordId) {
  return request({ url: `/workflow/lobster/ai-generator/result/${recordId}/detail`, method: 'get' })
}

export function confirmGenerate(recordId, data) {
  return request({ url: `/workflow/lobster/ai-generator/confirm/${recordId}`, method: 'post', data })
}

export function confirmGenerateEdited(recordId, data) {
  return request({ url: `/workflow/lobster/ai-generator/confirm/${recordId}/edited`, method: 'post', data })
}

export function regenerateWorkflow(recordId, data) {
  return request({ url: `/workflow/lobster/ai-generator/regenerate/${recordId}`, method: 'post', data })
}

// ======== 提示词管理 ========
export function listPrompts(params) {
  return request({ url: '/workflow/lobster/prompt/list', method: 'get', params })
}

export function getPrompt(id) {
  return request({ url: `/workflow/lobster/prompt/${id}`, method: 'get' })
}

export function addPrompt(data) {
  return request({ url: '/workflow/lobster/prompt', method: 'post', data })
}

export function updatePrompt(id, data) {
  return request({ url: `/workflow/lobster/prompt/${id}`, method: 'put', data })
}

export function deletePrompt(id) {
  return request({ url: `/workflow/lobster/prompt/${id}`, method: 'delete' })
}

export function getPromptCategories() {
  return request({ url: '/workflow/lobster/prompt/categories', method: 'get' })
}

export function refreshPromptCache() {
  return request({ url: '/workflow/lobster/prompt/refresh-cache', method: 'post' })
}

// ======== 销冠语料 ========
export function listSalesCorpus(params) {
  return request({ url: '/workflow/lobster/sales-corpus/list', method: 'get', params })
}

export function addCorpusDialog(data) {
  return request({ url: '/workflow/lobster/sales-corpus/dialog', method: 'post', data })
}

export function batchImportCorpus(data) {
  return request({ url: '/workflow/lobster/sales-corpus/batch-import', method: 'post', data })
}

export function analyzeCorpus() {
  return request({ url: '/workflow/lobster/sales-corpus/analyze', method: 'post' })
}

export function getCorpusScenarios() {
  return request({ url: '/workflow/lobster/sales-corpus/scenarios', method: 'get' })
}

// ======== API注册中心 ========
export function listApiRegistry() {
  return request({ url: '/workflow/lobster/api-registry/list', method: 'get' })
}

export function registerApi(data) {
  return request({ url: '/workflow/lobster/api-registry', method: 'post', data })
}

export function refreshApiCache() {
  return request({ url: '/workflow/lobster/api-registry/refresh', method: 'post' })
}

export function getApiCategories() {
  return request({ url: '/workflow/lobster/api-registry/categories', method: 'get' })
}

// ======== 死信队列 ========
export function listDeadLetters() {
  return request({ url: '/workflow/lobster/dead-letter/list', method: 'get' })
}

export function retryAllDeadLetters() {
  return request({ url: '/workflow/lobster/dead-letter/retry-all', method: 'post' })
}

export function getDeadLetterStats() {
  return request({ url: '/workflow/lobster/dead-letter/stats', method: 'get' })
}

// ======== 节点审核 ========
export function listEventAudits(params) {
  return request({ url: '/workflow/lobster/event-audit/list', method: 'get', params })
}

export function approveEvent(id) {
  return request({ url: `/workflow/lobster/event-audit/approve/${id}`, method: 'post' })
}

export function rejectEvent(id, data) {
  return request({ url: `/workflow/lobster/event-audit/reject/${id}`, method: 'post', data })
}

export function getEventAuditDetail(id) {
  return request({ url: `/workflow/lobster/event-audit/${id}`, method: 'get' })
}

// ======== 优化建议 ========
export function listPendingAudit(params) {
  return request({ url: '/workflow/lobster/optimization/pending-audit', method: 'get', params })
}

export function batchAuditOptimization(data) {
  return request({ url: '/workflow/lobster/optimization/batch-audit', method: 'post', data })
}

export function auditSingleOptimization(optimizationId, approved, remark) {
  return request({ url: `/workflow/lobster/optimization/audit/${optimizationId}`, method: 'post', params: { approved, remark } })
}

export function analyzeOptimization(externalUserId, workflowId) {
  return request({ url: '/workflow/lobster/optimization/analyze', method: 'post', params: { externalUserId, workflowId } })
}

export function getOptimizationStats() {
  return request({ url: '/workflow/lobster/optimization/stats', method: 'get' })
}

export function getOptimizationConfig(params) {
  return request({ url: '/workflow/lobster/optimization/config', method: 'get', params })
}

export function setOptimizationConfig(data) {
  return request({ url: '/workflow/lobster/optimization/config', method: 'post', data })
}

// ======== 模拟对话 ========
export function simulateChat(data) {
  return request({ url: '/workflow/simulate', method: 'post', data })
}

// ======== AI回复质量评分 ========
export function listQualityRecords(params) {
  return request({ url: '/aiChatQuality/list', method: 'get', params })
}

export function getQualityRecord(id) {
  return request({ url: `/aiChatQuality/${id}`, method: 'get' })
}

export function submitQualityReview(data) {
  return request({ url: '/aiChatQuality', method: 'post', data })
}

export function updateQualityReview(data) {
  return request({ url: '/aiChatQuality', method: 'put', data })
}

export function deleteQualityRecord(id) {
  return request({ url: `/aiChatQuality/${id}`, method: 'delete' })
}

export function getQualityStats(params) {
  return request({ url: '/aiChatQuality/stats', method: 'get', params })
}

// ======== 工作流模板获取 ========
export function getTemplateWithNodes(id) {
  return request({ url: `/workflow/template/${id}`, method: 'get' })
}

export function listAllTemplates() {
  return request({ url: '/workflow/lobster/template/list', method: 'get' })
}

// ======== 工作流模板与节点CRUD（画布编辑器） ========
export function listWorkflowTemplates() {
  return request({ url: '/workflow/lobster/template/list', method: 'get' })
}

export function getWorkflowNodes(workflowId) {
  return request({ url: `/workflow/lobster/nodes/${workflowId}`, method: 'get' })
}

export function saveWorkflowNodes(data) {
  return request({ url: '/workflow/lobster/nodes/save', method: 'post', data })
}

/* ========== 渠道聚合聊天 ========== */

export function getChatAggregate(params) {
  return request({ url: '/workflow/lobster/chat-aggregate', method: 'get', params })
}

export function getChatMessages(sessionId) {
  return request({ url: `/workflow/lobster/chat-aggregate/messages/${sessionId}`, method: 'get' })
}
