/**
 * 设备模拟模块 API 调用封装
 *
 * @author czj
 * @date 2026-07-29
 */

import request from '@/utils/request'
import type {
  EnvConfig,
  EnvConfigCreate,
  EnvConfigUpdate,
  EnvConfigListResponse,
  Simulator,
  SimulatorCreate,
  SimulatorUpdate,
  SimulatorListResponse,
  ReportLogListResponse,
  ManualReportRequest,
  TestConnectionResponse,
} from '@/types/device'

const BASE = '/api/device-sim'

// ==================== 环境配置 API ====================

/** 获取环境配置列表 */
export function listEnvConfigs(params?: { skip?: number; limit?: number }): Promise<EnvConfigListResponse> {
  return request.get(`${BASE}/env-configs`, { params })
}

/** 获取环境配置详情 */
export function getEnvConfig(id: string): Promise<EnvConfig> {
  return request.get(`${BASE}/env-configs/${id}`)
}

/** 创建环境配置 */
export function createEnvConfig(data: EnvConfigCreate): Promise<EnvConfig> {
  return request.post(`${BASE}/env-configs`, data)
}

/** 更新环境配置 */
export function updateEnvConfig(id: string, data: EnvConfigUpdate): Promise<EnvConfig> {
  return request.put(`${BASE}/env-configs/${id}`, data)
}

/** 删除环境配置 */
export function deleteEnvConfig(id: string): Promise<void> {
  return request.delete(`${BASE}/env-configs/${id}`)
}

/** 测试 MQTT 连接 */
export function testEnvConnection(id: string): Promise<TestConnectionResponse> {
  return request.post(`${BASE}/env-configs/${id}/test`)
}

// ==================== 模拟设备 API ====================

/** 获取模拟设备列表 */
export function listSimulators(params?: {
  skip?: number
  limit?: number
  deviceType?: string
  envConfigId?: string
  status?: string
}): Promise<SimulatorListResponse> {
  return request.get(`${BASE}/devices`, { params })
}

/** 获取模拟设备详情 */
export function getSimulator(id: string): Promise<Simulator> {
  return request.get(`${BASE}/devices/${id}`)
}

/** 创建模拟设备 */
export function createSimulator(data: SimulatorCreate): Promise<Simulator> {
  return request.post(`${BASE}/devices`, data)
}

/** 更新模拟设备 */
export function updateSimulator(id: string, data: SimulatorUpdate): Promise<Simulator> {
  return request.put(`${BASE}/devices/${id}`, data)
}

/** 删除模拟设备 */
export function deleteSimulator(id: string): Promise<void> {
  return request.delete(`${BASE}/devices/${id}`)
}

/** 启动模拟设备 */
export function startSimulator(id: string): Promise<{ message: string; id: string; status: string }> {
  return request.post(`${BASE}/devices/${id}/start`)
}

/** 停止模拟设备 */
export function stopSimulator(id: string): Promise<{ message: string; id: string; status: string }> {
  return request.post(`${BASE}/devices/${id}/stop`)
}

/** 手动触发上报 */
export function manualReport(id: string, data: ManualReportRequest): Promise<{ message: string; success: boolean }> {
  return request.post(`${BASE}/devices/${id}/report`, data)
}

// ==================== 上报记录 API ====================

/** 获取上报记录列表 */
export function listReportLogs(params?: {
  deviceId?: string
  direction?: string
  skip?: number
  limit?: number
  startTime?: string
  endTime?: string
}): Promise<ReportLogListResponse> {
  return request.get(`${BASE}/report-logs`, { params })
}

/** 清理过期上报记录 */
export function cleanupReportLogs(days: number = 7): Promise<{ message: string; cleanedCount: number }> {
  return request.delete(`${BASE}/report-logs/cleanup`, { params: { days } })
}