/** Matches agent/client FullSysCheckReport JSON */

export interface FullSysCheckReport {
  generated_at: string;
  platform: string;
  arch: string;
  os_version?: string;
  hostname?: string;
  worker_name?: string;
  build_id?: string;
  agent_id?: string;

  identity?: SysCheckIdentity;
  hardware?: SysCheckHardware;
  security?: SysCheckSecurity;
  network?: SysCheckNetwork;
  resources?: SysCheckResources;
  listen_ports?: { ports: SysCheckListenPort[]; count: number };
  patch?: SysCheckPatch;
  environment?: SysCheckEnvironment;
  neighbors?: SysCheckNeighbors;
  kev_exposure?: KEVScanReport;

  raw_sysinfo?: string;
  raw_ipconfig?: string;
  raw_netstat?: string;
  probe_errors?: string[];
}

export interface SysCheckIdentity {
  username?: string;
  domain?: string;
  computer_name?: string;
  agent_elevated?: boolean;
  mac_address?: string;
}

export interface SysCheckHardware {
  manufacturer?: string;
  model?: string;
  serial?: string;
  bios_version?: string;
  cpus?: { name?: string; cores?: number; logical?: number; max_mhz?: number; current_mhz?: number }[];
  memory_gb?: number;
  gpus?: { name?: string; driver?: string; vram_mb?: number }[];
  disks?: { mount?: string; label?: string; fs_type?: string; total_gb?: number; free_gb?: number; free_pct?: number }[];
  uptime_hours?: number;
}

export interface SysCheckSecurity {
  posture_score?: number;
  defender_enabled?: boolean;
  defender_rtp?: boolean;
  av_products?: string[];
  firewall_domain?: boolean;
  firewall_private?: boolean;
  firewall_public?: boolean;
  ssh_listening?: boolean;
  pending_updates?: number;
  last_patch?: string;
  last_patch_days?: number;
  reboot_pending?: boolean;
  services?: { name: string; display_name?: string; status: string; start_type: string }[];
}

export interface SysCheckNetwork {
  primary_local_ip?: string;
  external_ip?: string;
  external_ip_source?: string;
  geo?: {
    query?: string;
    country?: string;
    region?: string;
    city?: string;
    isp?: string;
    org?: string;
    lat?: number;
    lon?: number;
  };
  dns?: { servers?: string[]; search_domains?: string[] };
  interfaces?: { name?: string; mac?: string; ipv4?: string[]; ipv6?: string[] }[];
  default_gateway?: string;
  routes_summary?: string;
}

export interface SysCheckResources {
  cpu_freq_mhz?: number;
  cpu_max_mhz?: number;
  cpu_throttle?: boolean;
  cpu_temp_c?: number;
  disk_free_gb?: number;
  disk_total_gb?: number;
  disk_free_pct?: number;
  gpu_temp_c?: number;
  gpu_usage_pct?: number;
}

export interface SysCheckListenPort {
  port: number;
  addr?: string;
  proto?: string;
  process?: string;
  pid?: number;
}

export interface SysCheckPatch {
  pending_updates?: number;
  last_patch?: string;
  last_patch_days?: number;
  reboot_pending?: boolean;
}

export interface SysCheckEnvironment {
  timezone?: string;
  locale?: string;
  home_dir?: string;
  temp_dir?: string;
  install_dir?: string;
}

export interface KEVFinding {
  cve: string;
  name: string;
  product?: string;
  severity: string;
  cisa_kev?: boolean;
  status: 'exposed' | 'likely' | 'clear' | 'n/a';
  detail?: string;
}

export interface KEVScanReport {
  scanned_at: string;
  exposed_count: number;
  likely_count: number;
  critical_count: number;
  risk_score: number;
  summary?: string;
  findings: KEVFinding[];
}

export interface SysCheckNeighbors {
  arp_hosts?: string[];
  subnet_scan?: string;
  arp_count?: number;
}

export function parseFullSysCheckMessage(message: string): FullSysCheckReport | null {
  const start = message.indexOf('{');
  if (start < 0) return null;
  try {
    return JSON.parse(message.slice(start)) as FullSysCheckReport;
  } catch {
    return null;
  }
}
