# Capture Nexus Shell visual evidence screenshots (build 13)
$ErrorActionPreference = "Stop"
$repo = "C:\Users\jtrs\Documents\renninjadk\Ninja Nexus"
$evidenceDir = Join-Path $repo "docs\evidence\nexus-visual-shell-polish"
$shellDir = Join-Path $repo "source\nexus-shell\NexusShell\bin\Release\net10.0-windows"
$identityDir = Join-Path $repo "source\nexus-identity\NexusIdentity.Api"
$build = (Get-Content (Join-Path $repo "source\nexus-shell\NexusShell\config\version.poc.json") | ConvertFrom-Json).build

New-Item -ItemType Directory -Force -Path $evidenceDir | Out-Null

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

Add-Type @"
using System;
using System.Runtime.InteropServices;
public static class NexusMouse {
    [DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y);
    [DllImport("user32.dll")] public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    public static void LeftClick(int x, int y) {
        SetCursorPos(x, y);
        mouse_event(0x0002, 0, 0, 0, 0);
        mouse_event(0x0004, 0, 0, 0, 0);
    }
}
"@

function Save-Screenshot([string]$path) {
    Start-Sleep -Milliseconds 900
    $screen = [System.Windows.Forms.Screen]::PrimaryScreen
    $bmp = New-Object System.Drawing.Bitmap $screen.Bounds.Width, $screen.Bounds.Height
    $g = [System.Drawing.Graphics]::FromImage($bmp)
    $g.CopyFromScreen($screen.Bounds.Location, [Drawing.Point]::Empty, $screen.Bounds.Size)
    $bmp.Save($path, [System.Drawing.Imaging.ImageFormat]::Png)
    $g.Dispose()
    $bmp.Dispose()
    Write-Host "Saved $path"
}

function Wait-NexusWindow([int]$timeoutSec = 30) {
    $deadline = (Get-Date).AddSeconds($timeoutSec)
    while ((Get-Date) -lt $deadline) {
        $p = Get-Process -Name NexusShell -ErrorAction SilentlyContinue | Select-Object -First 1
        if ($p -and $p.MainWindowHandle -ne 0) { return $p }
        Start-Sleep -Milliseconds 400
    }
    throw "NexusShell window not found"
}

if (-not (Get-Process -Name "NexusIdentity.Api" -ErrorAction SilentlyContinue)) {
    Start-Process -FilePath "dotnet" -WorkingDirectory $identityDir `
        -ArgumentList @("run","--no-build","-c","Release","--urls","http://localhost:5050") -WindowStyle Hidden
    Start-Sleep -Seconds 5
}

Stop-Process -Name NexusShell -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1
Start-Process -FilePath (Join-Path $shellDir "NexusShell.exe") -WorkingDirectory $shellDir -ArgumentList @("--safe-poc")
$proc = Wait-NexusWindow 25
Start-Sleep -Seconds 2

[System.Windows.Forms.SendKeys]::SendWait("user@nexus.local{TAB}NexusUser1!{ENTER}")
Start-Sleep -Seconds 4

Save-Screenshot (Join-Path $evidenceDir "build-$build-desktop.png")

[System.Windows.Forms.SendKeys]::SendWait("%k")
Start-Sleep -Seconds 1
Save-Screenshot (Join-Path $evidenceDir "build-$build-launcher.png")

[System.Windows.Forms.SendKeys]::SendWait("{ESC}")
Start-Sleep -Milliseconds 700

$screen = [System.Windows.Forms.Screen]::PrimaryScreen
$clickX = [int]($screen.Bounds.Width * 0.92)
$clickY = [int]($screen.Bounds.Height * 0.04)
[NexusMouse]::LeftClick($clickX, $clickY)
Start-Sleep -Seconds 1
Save-Screenshot (Join-Path $evidenceDir "build-$build-notifications.png")

Write-Host "Evidence capture complete for build $build"
