/**
 * API response type definitions for Nexus backend
 *
 * Centralizes all response shapes to eliminate `as any` casts.
 */

/** Generic paginated list response — most list endpoints return this */
export interface PaginatedResponse<T> {
  items: T[]
  total: number
  limit?: number
  offset?: number
}

/** Settings list response — backend returns flat array */
export type SettingsResponse = SettingItem[]

export interface SettingItem {
  key: string
  value: unknown
  updated_at?: string
}

/** IP Allowlist response */
export interface AllowlistResponse {
  enabled: boolean
  subscription_url?: string
  subscription_ips?: string[]
  manual_ips?: string[]
  subscription_count?: number
  manual_count?: number
  total_count?: number
  last_refresh?: string | null
}

/** File browse response from POST /api/sync/browse */
export interface BrowseResponse {
  path?: string
  items: FileEntry[]
  /** SSH/listing failure message when items is empty */
  error?: string
}

export interface FileEntry {
  name: string
  type: 'directory' | 'file' | 'symlink'
  size: number
  modified: string
  permissions: string
  owner?: string
  group?: string
  mode_octal?: string
  link_target?: string | null
}

export type FilesElevationMode = 'off' | 'auto_sudo' | 'always_sudo'

export interface FilesCapabilityResult {
  ssh_user: string
  is_root: boolean
  files_elevation: FilesElevationMode
  can_sudo_nopasswd: boolean
  whitelist_ok: boolean
  message: string
  docs_path: string
}

/** Server item from /api/servers/ */
export interface ServerApiItem {
  id: number
  name: string
  domain: string
  port: number
  username: string
  auth_method: string
  password_set: boolean
  ssh_key_path: string
  ssh_key_public: string
  ssh_key_private_set: boolean
  agent_port: number
  agent_api_key: string
  agent_api_key_set: boolean
  preset_id: number | null
  ssh_key_preset_id: number | null
  description: string
  target_path: string
  category: string | null
  platform_id: number | null
  node_id: number | null
  files_elevation?: FilesElevationMode
  is_online: boolean
  last_heartbeat: string | null
  agent_version: string | null
  status: string
  created_at: string | null
  updated_at: string | null
  system_info?: {
    cpu_usage?: number
    mem_usage?: number
    disk_usage?: number
    [key: string]: unknown
  }
  connectivity?: string
  ssh_key_configured?: boolean
  last_checked_at?: string | null
  protocols?: Record<string, unknown>
  extra_attrs?: Record<string, unknown>
  _source?: string
}

/** Alert history item from /api/alert-history/ */
export interface AlertLogItem {
  id: number
  server_id: number
  server_name: string
  alert_type: string
  value: string
  is_recovery: boolean
  created_at: string | null
}

/** Command log item from /api/assets/command-logs */
export interface CommandLogItem {
  id: number
  session_id: string | null
  server_id: number
  server_name: string | null
  admin_id: number
  admin_username: string | null
  command: string
  remote_addr: string | null
  created_at: string | null
}

/** SSH session item from /api/assets/ssh-sessions */
export interface SshSessionItem {
  id: number
  server_id: number
  server_name: string | null
  admin_id: number
  remote_addr: string | null
  status: string
  started_at: string | null
  closed_at: string | null
}

/** Retry item from /api/retries/ */
export interface RetryItem {
  id: number
  server_id: number
  server_name: string
  operation: string
  status: string
  last_error: string | null
  operator?: string
  source_path?: string
  target_path?: string
  max_retries?: number
  next_retry_at?: string | null
  retry_count: number
  created_at: string | null
}

/** Script item from /api/scripts/ */
export interface ScriptItem {
  id: number
  name: string
  description: string
  content: string
  script_type: string
  created_at: string | null
  updated_at: string | null
}

/** Push history item from /api/sync/push-history or /api/sync/history */
export interface PushItem {
  id: number
  batch_id: string
  server_id: number
  server_name: string
  source_path: string
  target_path: string
  status: string
  created_at: string | null
}

/** Schedule item from /api/schedules/ */
export interface ScheduleItem {
  id: number
  name: string
  cron_expr: string
  command?: string
  source_path?: string
  target_path?: string
  server_ids?: number[]
  run_mode?: string
  sync_mode?: string
  schedule_type?: string
  script_id?: number | null
  script_content?: string
  exec_timeout?: number
  long_task?: boolean
  enabled: boolean
  last_run_at: string | null
  next_run: string | null
  created_at: string | null
}

/** Audit log item from /api/audit/ */
export interface AuditItem {
  id: number
  admin_username: string
  action: string
  /** API 字段为 target_type；兼容旧字段名 */
  resource_type: string
  target_type?: string
  resource_id: number | null
  target_id?: number | null
  detail: string | null
  ip_address: string | null
  created_at: string | null
}

/** Script execution item from /api/scripts/executions */
export interface ScriptExecItem {
  id: number
  script_id: number
  command: string
  server_ids: string
  status: string
  results: Record<string, { status?: string; output?: string; exit_code?: number }>
  events: Array<{ event: string; ts: string }>
  operator: string | null
  started_at: string | null
  completed_at: string | null
  long_task?: boolean
  progress?: { total: number; done: number; succeeded: number; failed: number }
  credential_id?: number | null
  source: string
}

/** Alert history stats from /api/alert-history/stats */
export interface AlertStatsResponse {
  today: number
  active: number
  recovered: number
  top_server: string
  top_types?: { alert_type: string; count: number }[]
  top_servers?: { server_id: number; server_name: string; count: number }[]
  daily?: { day: string; alerts: number; recoveries: number }[]
}
