# Verify Nexus Laptop POC package integrity and readiness.
param(
  [string]$InstallRoot = $PSScriptRoot,
  [switch]$SkipRuntimeChecks
)

$ErrorActionPreference = "Stop"
$failed = 0
$warned = 0

function Test-Check {
  param([string]$Name, [scriptblock]$Block, [switch]$Optional)
  try {
    & $Block
    Write-Host "[PASS] $Name" -ForegroundColor Green
  } catch {
    if ($Optional) {
      Write-Host "[WARN] $Name - $($_.Exception.Message)" -ForegroundColor Yellow
      $script:warned++
    } else {
      Write-Host "[FAIL] $Name - $($_.Exception.Message)" -ForegroundColor Red
      $script:failed++
    }
  }
}

Write-Host "=== VERIFY NEXUS POC ===" -ForegroundColor Cyan
Write-Host "Root: $InstallRoot"

Test-Check "shell/NexusShell.exe exists" {
  if (-not (Test-Path (Join-Path $InstallRoot "shell\NexusShell.exe"))) { throw "missing" }
}
Test-Check "identity/NexusIdentity.Api.exe exists" {
  if (-not (Test-Path (Join-Path $InstallRoot "identity\NexusIdentity.Api.exe"))) { throw "missing" }
}
Test-Check "lockdown.poc.json exists" {
  if (-not (Test-Path (Join-Path $InstallRoot "shell\config\lockdown.poc.json"))) { throw "missing" }
}
Test-Check "developer.poc.json exists" {
  if (-not (Test-Path (Join-Path $InstallRoot "shell\config\developer.poc.json"))) { throw "missing" }
}
Test-Check "lockdown config parses" {
  $cfg = Get-Content (Join-Path $InstallRoot "shell\config\lockdown.poc.json") -Raw | ConvertFrom-Json
  if (-not $cfg.lockdownMode) { throw "lockdownMode not true" }
  if ($cfg.allowedApps.Count -lt 1) { throw "no allowedApps" }
  if ($cfg.blockedApps.Count -lt 1) { throw "no blockedApps" }
}
Test-Check "START script exists" {
  if (-not (Test-Path (Join-Path $InstallRoot "START-NEXUS-SAFE-MODE.ps1"))) { throw "missing" }
}
Test-Check "STOP script exists" {
  if (-not (Test-Path (Join-Path $InstallRoot "STOP-NEXUS.ps1"))) { throw "missing" }
}
Test-Check "Developer Console shortcut documented" {
  $readme = Get-Content (Join-Path $InstallRoot "README-FIRST.txt") -Raw
  if ($readme -notmatch "Ctrl\+Shift\+Alt\+N") { throw "shortcut not documented" }
}

if (-not $SkipRuntimeChecks) {
  Test-Check ".NET 8 runtime for Identity" {
    $dotnet = Get-Command dotnet -ErrorAction SilentlyContinue
    if (-not $dotnet) { throw "dotnet CLI not found" }
    $runtimes = & dotnet --list-runtimes 2>&1 | Out-String
    if ($runtimes -notmatch "Microsoft\.NETCore\.App 8\.") { throw ".NET 8 runtime missing" }
  } -Optional
  Test-Check ".NET 10 Desktop runtime for Shell" {
    $runtimes = & dotnet --list-runtimes 2>&1 | Out-String
    if ($runtimes -notmatch "Microsoft\.WindowsDesktop\.App 10\.") { throw ".NET 10 Desktop runtime missing" }
  } -Optional
}

Write-Host ""
if ($failed -eq 0) {
  Write-Host "All required checks passed. Warnings: $warned" -ForegroundColor Green
  exit 0
} else {
  Write-Host ("{0} required check(s) failed. Warnings: {1}" -f $failed, $warned) -ForegroundColor Red
  exit 1
}
