/**
 * get-network-discovery — local IP config + ARP table from the WNP. Site-
 * anchored. Designed to catch the IP-conflict pattern (the proven test
 * case at Hynes had 10.10.110.215 / .253 fought over by two VMware MACs).
 *
 * Output: own NIC config + parsed ARP table (IP, MAC, type) + a flag for
 * any duplicate IPs seen in ARP responses.
 */

import { type RmmScript, parseJsonOutput } from './types';

export const getNetworkDiscovery: RmmScript = {
  id: 'get-network-discovery',
  name: 'Network discovery (NIC config + ARP)',
  description:
    'Local NIC IPv4 configuration plus full ARP table (parsed). Flags duplicate IPs seen across MACs.',
  target_type: 'site_anchor',
  expected_runtime_seconds: 20,
  version: 1,
  body: `
$ErrorActionPreference = 'SilentlyContinue'
$nics = Get-NetIPConfiguration |
  Where-Object { $_.NetAdapter.Status -eq 'Up' } |
  ForEach-Object {
    [pscustomobject]@{
      interface_alias = $_.InterfaceAlias
      ipv4_address    = ($_.IPv4Address.IPAddress -join ',')
      ipv4_default_gateway = ($_.IPv4DefaultGateway.NextHop -join ',')
      dns_servers     = ($_.DNSServer | Where-Object { $_.AddressFamily -eq 2 }).ServerAddresses -join ','
      mac             = $_.NetAdapter.LinkLayerAddress
      link_speed      = $_.NetAdapter.LinkSpeed
    }
  }

$arpRaw = & arp -a 2>&1
$arp = @()
$dup = @{}
foreach ($line in $arpRaw) {
  if ($line -match '^\\s*(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s+([a-fA-F0-9:-]{11,17})\\s+(\\w+)') {
    $ip   = $Matches[1]
    $mac  = $Matches[2].ToLower()
    $type = $Matches[3]
    $arp += [pscustomobject]@{ ip = $ip; mac = $mac; type = $type }
    if ($dup.ContainsKey($ip)) { $dup[$ip] = ($dup[$ip] + ',' + $mac) }
    else { $dup[$ip] = $mac }
  }
}
$conflicts = @()
foreach ($k in $dup.Keys) {
  $macs = ($dup[$k] -split ',') | Sort-Object -Unique
  if ($macs.Count -gt 1) {
    $conflicts += [pscustomobject]@{ ip = $k; macs = $macs }
  }
}

@{
  hostname = $env:COMPUTERNAME
  captured_at_utc = (Get-Date).ToUniversalTime().ToString("o")
  nics = $nics
  arp_count = $arp.Count
  arp = $arp
  ip_conflicts = $conflicts
  ip_conflict_count = $conflicts.Count
} | ConvertTo-Json -Depth 5 -Compress
`.trim(),
  parseOutput: (stdout) => parseJsonOutput(stdout),
};
