/**
 * API通用响应类型
 * @template T
 * data 的具体类型
 */
export interface ApiResponse<T = unknown> {
  /** 响应码，0 表示成功，其他表示错误 */
  code: number
  /** 响应信息描述 */
  message: string
  /** 业务数据 */
  data: T
}

/**
 * 分页通用数据结构
 */
export interface PaginatedResponse<T> {
  /** 结果项集合 */
  items: T[]
  /** 总记录数 */
  total: number
  /** 当前页码 */
  page: number
  /** 每页条数 */
  pageSize: number
}

// =================== 业务实体模型 ===================
/** 机床 */
export interface Machine {
  id: number
  name: string
  deviceCode: string
  workshopName: string
  brandName: string
  ipAddress: string
  isOnline: boolean
  isEnabled: boolean
  workerName?: string
  collectAddressName?: string
  /** 编辑表单用：品牌ID */
  brandId?: number
  /** 编辑表单用：所属车间ID */
  workshopId?: number
  /** 编辑表单用：采集地址ID */
  collectAddressId?: number
  /** 编辑表单用：绑定工人ID */
  workerId?: number
}

/** 机床状态 */
export interface MachineStatus {
  programName: string
  partCount: number
  runStatus: string
  operationMode: string
  /** 主轴设定转速 */
  spindleSpeedSet?: number
  /** 进给设定速度 */
  feedSpeedSet?: number
  /** 主轴实际转速 */
  spindleSpeedActual?: number
  spindleSpeed: number
  feedRate: number
  spindleLoad: number
  machiningStatus: string
  lastCollectTime: string
}

/** 品牌 */
export interface Brand {
  id: number
  brandName: string
  deviceField: string
  tagsPath: string
  isEnabled: boolean
  fieldCount: number
}

/** 品牌字段映射 */
export interface BrandFieldMapping {
  id?: number
  standardField: string
  fieldName: string
  matchMethod: string
  dataType: string
  required: boolean
}

/** 采集地址 */
export interface CollectAddress {
  id: number
  name: string
  url: string
  brandId: number
  brandName: string
  interval: number
  /** 详情页显示用：采集间隔（秒） */
  collectInterval?: number
  isEnabled: boolean
  lastCollectTime: string
  machineCount: number
  failCount: number
  /** 详情页：原始JSON */
  rawJson?: string
}

/** 工人 */
export interface Worker {
  id: number
  code: string
  name: string
  isEnabled: boolean
  machineCount: number
  machines?: Machine[]
}

/** 告警 */
export interface Alert {
  id: number
  alertType: string
  machineName: string
  message: string
  title?: string
  detail?: string
  isResolved: boolean
  createdAt: string
  resolvedAt?: string
}

/** 产量记录 */
export interface ProductionRecord {
  id: number
  date: string
  machineName: string
  programName: string
  quantity: number
  runTime: number
  cuttingTime: number
  dayStatus: string
  adjusted: boolean
}

/** 修正历史 */
export interface AdjustmentHistory {
  id: number
  adjustedAt: string
  beforeQuantity: number
  afterQuantity: number
  reason: string
  operatorName: string
}

/** 系统配置 */
export interface SysConfig {
  id: number
  configKey: string
  configValue: string
  valueType: 'string' | 'number'
  description: string
  isSensitive: boolean
}

/** 车间 */
export interface Workshop {
  id: number
  name: string
  description?: string
}

/** 操作日志 */
export interface OperationLog {
  id: number
  /** 日志时间 */
  createdAt: string
  /** 日志级别（ERROR/WARN/INFO/DEBUG） */
  logLevel: string
  source: string
  message: string
  stackTrace?: string
  extraData?: string
  /** 产量修正日志扩展字段 */
  targetTable?: string
  targetId?: number
  oldValue?: number
  newValue?: number
  reason?: string
  operatorIp?: string
}

/** 大屏卡片配置 */
export interface ScreenCard {
  id: number
  cardName: string
  cardKey: string
  cardType: string
  metric: string
  dimension?: string
  sortOrder: number
  isEnabled: boolean
  chartConfig?: string
  /** 前端展示用标题（同 cardName） */
  title?: string
  /** 前端展示用（同 cardKey） */
  screenKey?: string
  filterType?: string
  filterValue?: string
  isDefault?: boolean | number
}

/** 大屏筛选配置 */
export interface ScreenFilter {
  id: number
  filterKey: string
  filterType: string
  filterValues: string
  isDefault: boolean
  sortOrder: number
}

/** 仪表盘统计 */
export interface DashboardSummary {
  onlineCount: number
  totalMachines: number
  todayProduction: number
  activeAlerts: number
  /** 今日采集成功率(%) */
  collectSuccessRate: number
  /** 运行中机床数(有程序运行) */
  runningMachines: number
  /** 数据缺失机床数(在线但采集失败) */
  dataMissingMachines: number
}

// 新增：采集服务状态数据结构（后端扩大了 serviceStatus 等字段）
export interface CollectorStatus {
  status: string; // 原心跳状态文本，保留旧字段
  uptimeSeconds?: number;
  lastCollectTime?: string | null;
  serviceStatus?: string; // Windows 服务状态，如 NotInstalled、Running、Starting、StartFailed、Stopped
  serviceName?: string;
  serviceMessage?: string;
}

/** 仪表盘产量趋势 */
export interface DashboardTrendItem {
  date: string
  quantity: number
}

/** 车间平均单机产量 */
export interface WorkshopProduction {
  workshopName: string
  quantity: number
  machineCount: number
  avgQuantity: number
}

/** 机床状态分布 */
export interface MachineStatusDistribution {
  online: number
  offline: number
  disabled: number
}

/** 最新告警(仪表盘) */
export interface RecentAlert {
  id: number
  createdAt: string
  alertType: string
  machineName: string
  title: string
  isResolved: boolean
}

// 机床产量排行榜行数据
export interface MachineRankRow {
  rank?: number
  id?: number
  machineName: string
  quantity?: number
  status?: number
  program?: string
}

// 工人产量排行榜行数据
export interface WorkerRankRow {
  rank?: number
  id?: number
  workerName: string
  machineCount?: number
  totalQuantity?: number
}

// CollectorStatus 已在上方定义（含 serviceStatus/serviceName/serviceMessage 扩展字段）

/** 产量看板汇总 */
export interface ProductionDashboardSummary {
  totalQuantity: number
  activeMachineCount: number
  totalCuttingTime: number
  avgQuantityPerMachine: number
}
