/**
 * 服务监测 API 类型
 */

// ============================================================
// 监测目标
// ============================================================

export interface Target {
  id: string
  name: string
  type: 'local' | 'remote'
  host: string
  port: number
  username: string
  has_password: boolean
  built_in: boolean
  container_overrides: Record<string, string>
  thresholds: Record<string, number | string>
  warning?: string  // 本级目标警告信息
  created_at?: string
  updated_at?: string
}

export interface CreateTargetRequest {
  name: string
  host: string
  port?: number
  username: string
  password: string
  container_overrides?: Record<string, string>
  credential_overrides?: Record<string, string>
  thresholds?: Record<string, number | string>
}

export interface UpdateTargetRequest {
  name?: string
  host?: string
  port?: number
  username?: string
  password?: string
  container_overrides?: Record<string, string>
  credential_overrides?: Record<string, string>
  thresholds?: Record<string, number | string>
}

export interface TestConnectionRequest {
  host: string
  port?: number
  username: string
  password: string
}

export interface TestConnectionResponse {
  success: boolean
  message: string
  detail: string | null
  error_code: string | null
}

// ============================================================
// 巡检执行
// ============================================================

export interface RunStatusResponse {
  running: boolean
  run_id?: string
  target_id?: string
  suite?: string
  done?: number
  total?: number
  current?: string
}

export interface ModuleSummary {
  正常: number
  警告: number
  严重: number
  total: number
}

export interface InspectProgressEvent {
  event: 'progress' | 'module_done' | 'finished' | 'error' | 'cancelled'
  run_id: string
  done?: number
  total?: number
  current?: string
  module?: string
  items_count?: number
  summary?: ModuleSummary
  report_id?: string
  message?: string
}

// ============================================================
// 巡检报告
// ============================================================

export interface ReportSummary {
  id: string
  target_id: string
  target_name: string
  target_type: string
  suite: 'quick' | 'full'
  started_at: string
  finished_at: string
  summary: ModuleSummary
}

export interface CheckItem {
  key: string
  name: string
  value: string
  threshold: string
  status: '正常' | '警告' | '严重'
}

export interface ModuleResult {
  id: string
  name: string
  category: string
  items: CheckItem[]
  summary: ModuleSummary
}

export interface ReportDetail {
  id: string
  target_id: string
  target_name: string
  target_type: string
  suite: 'quick' | 'full'
  started_at: string
  finished_at: string
  summary: ModuleSummary
  modules: ModuleResult[]
  access_token: string
  access_token_expires_at: string
}

export interface BatchDeleteReportsRequest {
  ids: string[]
}

// ============================================================
// 报告对比
// ============================================================

export interface CompareReportSummary {
  id: string
  target_id: string
  target_name: string
  target_type: string
  suite: string
  created_at: string
  total_items: number
  normal_count: number
  warning_count: number
  critical_count: number
}

export interface DiffItem {
  key: string
  name: string
  module_id: string
  module_name: string
  diff_type: 'new_abnormal' | 'recovered' | 'value_changed' | 'new_item' | 'removed_item'
  value_a: string
  value_b: string
  status_a: string
  status_b: string
  change_desc: string
}

export interface DiffGroup {
  module_id: string
  module_name: string
  items: DiffItem[]
}

export interface CompareResult {
  report_a: CompareReportSummary
  report_b: CompareReportSummary
  diff_summary: {
    new_abnormal: number
    recovered: number
    value_changed: number
    new_items: number
    removed_items: number
  }
  diff_details: {
    new_abnormal: DiffGroup[]
    recovered: DiffGroup[]
    value_changed: DiffGroup[]
    new_items: DiffGroup[]
    removed_items: DiffGroup[]
  }
}

// ============================================================
// 定时任务
// ============================================================

export interface Schedule {
  id: string
  name: string
  target_id: string
  target_name: string
  suite: 'quick' | 'full'
  cron: string
  repeat_mode: 'daily' | 'weekday' | 'weekly'
  hour: number
  minute: number
  weekdays: number[]
  start_date: string | null
  end_date: string | null
  enabled: boolean
  current_status: 'idle' | 'running' | 'success' | 'failed' | 'error'
  last_run_at: string | null
  last_run_status: string | null
  last_report_id: string | null
  next_run_at: string | null
  created_at: string
  created_by: string
}

export interface CreateScheduleRequest {
  name: string
  target_id: string
  suite?: 'quick' | 'full'
  repeat_mode?: 'daily' | 'weekday' | 'weekly'
  hour?: number
  minute?: number
  weekdays?: number[]
  start_date?: string
  end_date?: string
}

export type UpdateScheduleRequest = Partial<CreateScheduleRequest>

// ============================================================
// 通知配置
// ============================================================

export interface EmailConfig {
  enabled: boolean
  smtp_host: string
  smtp_port: number
  smtp_user: string
  smtp_password: string
  use_ssl: boolean
  recipients: string[]
  subject_template: string
}

export interface DingTalkConfig {
  enabled: boolean
  webhook_url: string
  secret: string
  at_mobiles: string[]
}

export interface WeComConfig {
  enabled: boolean
  webhook: string
}

export interface NotificationTrigger {
  on_complete: boolean
  on_abnormal_only: boolean
  include_details: boolean
  include_link: boolean
}

export interface AlertOnConsecutiveConfig {
  enabled: boolean
  consecutive_threshold: number
  alert_level: 'critical' | 'all'
  channels: string[]
}

export interface NotificationConfig {
  email: EmailConfig
  dingtalk: DingTalkConfig
  wecom: WeComConfig
  trigger: NotificationTrigger
  alert_on_consecutive?: AlertOnConsecutiveConfig
}

export interface NotificationConfigResponse {
  success: boolean
  config: NotificationConfig & {
    updated_at: string | null
    updated_by: string | null
  }
}

// ============================================================
// 监测统计
// ============================================================

export interface OverviewStats {
  total_reports: number
  total_items: number
  normal_count: number
  warning_count: number
  critical_count: number
  abnormal_rate: number
  most_abnormal_item: {
    key: string
    name: string
    abnormal_count: number
  } | null
  date_range: {
    start: string
    end: string
  }
}

export interface ModuleStats {
  id: string
  name: string
  category: string
  total: number
  normal: number
  warning: number
  critical: number
  pass_rate: number
}

export interface ItemTrendDataPoint {
  time: string
  value_num: number | null
  value_raw: string
  status: '正常' | '警告' | '严重'
}

export interface ItemTrendData {
  item_key: string
  item_name: string
  unit: string
  threshold: string
  data_points: ItemTrendDataPoint[]
  status_distribution: {
    正常: number
    警告: number
    严重: number
  }
}

export interface AbnormalItem {
  key: string
  name: string
  module_name: string
  category: string
  total_appearances: number
  abnormal_count: number
  abnormal_rate: number
  warning_count: number
  critical_count: number
}

export interface ItemKeyInfo {
  key: string
  name: string
  has_numeric: boolean
}

export interface ItemKeysModule {
  module_id: string
  module_name: string
  category: string
  items: ItemKeyInfo[]
}