/**
 * 设备模拟模块类型定义
 *
 * @author czj
 * @date 2026-07-29
 */

/** 设备类型枚举 */
export type DeviceType = 'door' | 'paperless' | 'central' | 'client'

/** 设备类型标签映射 */
export const DEVICE_TYPE_LABELS: Record<DeviceType, string> = {
  door: '门口屏',
  paperless: '无纸化',
  central: '中控设备',
  client: '集控客户端',
}

/** 设备类型图标映射 */
export const DEVICE_TYPE_ICONS: Record<DeviceType, string> = {
  door: 'Monitor',
  paperless: 'Document',
  central: 'Setting',
  client: 'Computer',
}

/** 环境配置 */
export interface EnvConfig {
  id: string
  name: string
  brokerHost: string
  brokerPort: number
  username: string | null
  password: string | null
  topicPrefix: string
  topics: Record<string, string> | null
  useTls: boolean
  clientIdPrefix: string
  defaultTopicParams: {
    company_id?: string
    room_ids?: string[]
    app_token_prefix?: string
    app_token_start?: number
    app_token_end?: number
    paperless_token_prefix?: string
    paperless_token_start?: number
    paperless_token_end?: number
    conference_id?: string
    conference_name?: string
  } | null
  status: string
  lastConnectedAt: string | null
  createdAt: string
  updatedAt: string
}

/** 创建环境配置请求 */
export interface EnvConfigCreate {
  name: string
  brokerHost: string
  brokerPort?: number
  username?: string | null
  password?: string | null
  topicPrefix?: string
  topics?: Record<string, string> | null
  useTls?: boolean
  clientIdPrefix?: string
  defaultTopicParams?: {
    company_id?: string
    room_ids?: string[]
    app_token_prefix?: string
    app_token_start?: number
    app_token_end?: number
    paperless_token_prefix?: string
    paperless_token_start?: number
    paperless_token_end?: number
    conference_id?: string
    conference_name?: string
  } | null
}

/** 更新环境配置请求 */
export interface EnvConfigUpdate {
  name?: string
  brokerHost?: string
  brokerPort?: number
  username?: string | null
  password?: string | null
  topicPrefix?: string
  topics?: Record<string, string> | null
  useTls?: boolean
  clientIdPrefix?: string
  defaultTopicParams?: {
    company_id?: string
    room_ids?: string[]
    app_token_prefix?: string
    app_token_start?: number
    app_token_end?: number
    paperless_token_prefix?: string
    paperless_token_start?: number
    paperless_token_end?: number
    conference_id?: string
    conference_name?: string
  } | null
}

/** 模拟设备 */
export interface Simulator {
  id: string
  envConfigId: string
  deviceType: DeviceType
  deviceName: string
  deviceId: string
  status: string
  autoReconnect: boolean
  extraAttrs: Record<string, any> | null
  topicParams: Record<string, string> | null
  reportConfig?: {
    interval: number      // 上报间隔（秒）
    enabled?: boolean     // 是否启用定时上报
  }
  lastReportedAt: string | null
  totalReports: number
  createdAt: string
  updatedAt: string
}

/** 创建模拟设备请求 */
export interface SimulatorCreate {
  envConfigId: string
  deviceType: DeviceType
  deviceName: string
  deviceId: string
  autoReconnect?: boolean
  extraAttrs?: Record<string, any> | null
  topicParams?: Record<string, string> | null
  reportConfig?: {
    interval: number
    enabled?: boolean
  }
}

/** 更新模拟设备请求 */
export interface SimulatorUpdate {
  deviceName?: string
  autoReconnect?: boolean
  extraAttrs?: Record<string, any> | null
  topicParams?: Record<string, string> | null
  reportConfig?: {
    interval: number
    enabled?: boolean
  }
}

/** 上报记录 */
export interface ReportLog {
  id: string
  deviceId: string
  topic: string
  payload: Record<string, any> | null
  direction: string
  status: string
  errorMessage: string | null
  createdAt: string
}

/** 手动上报请求 */
export interface ManualReportRequest {
  topic: string
  payload: Record<string, any>
}

/** 连接测试结果 */
export interface TestConnectionResponse {
  success: boolean
  message: string
  latencyMs: number | null
}

/** 环境配置列表响应 */
export interface EnvConfigListResponse {
  items: EnvConfig[]
  total: number
}

/** 模拟设备列表响应 */
export interface SimulatorListResponse {
  items: Simulator[]
  total: number
}

/** 上报记录列表响应 */
export interface ReportLogListResponse {
  items: ReportLog[]
  total: number
}

/** 主题模板 */
export interface TopicTemplate {
  key: string
  template: string
  label: string
  params: string[]
  paramLabels: Record<string, string>
  paramDefaults: Record<string, string>
  direction: 'publish' | 'subscribe'
}

/** 主题模板列表响应 */
export interface TopicTemplateListResponse {
  deviceType: string
  templates: TopicTemplate[]
  hasRealTopics: boolean
}

/** 设备导入行结果 */
export interface DeviceImportRowResult {
  row: number
  deviceName: string
  deviceId: string
  success: boolean
  error: string
  createdId: string | null
}

/** 设备导入响应 */
export interface DeviceImportResponse {
  total: number
  successCount: number
  failedCount: number
  results: DeviceImportRowResult[]
  message: string
}