# Capture dock-only evidence screenshots (build 15+)
$ErrorActionPreference = "Stop"
$repo = "C:\Users\jtrs\Documents\renninjadk\Ninja Nexus"
$evidenceDir = Join-Path $repo "docs\evidence\nexus-dock-polish"
$shellDir = Join-Path $repo "source\nexus-shell\NexusShell\bin\Release\net10.0-windows"
$identityDir = Join-Path $repo "source\nexus-identity\NexusIdentity.Api"

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;
using System.Text;
public static class Win32Focus {
    [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
    public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
    [DllImport("user32.dll")] public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
    [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
    public static IntPtr FindWindowByTitle(string titlePart) {
        IntPtr found = IntPtr.Zero;
        EnumWindows((hWnd, _) => {
            if (!IsWindowVisible(hWnd)) return true;
            var sb = new StringBuilder(256);
            GetWindowText(hWnd, sb, sb.Capacity);
            if (sb.ToString().Contains(titlePart)) { found = hWnd; return false; }
            return true;
        }, IntPtr.Zero);
        return found;
    }
}
"@

function Focus-NexusWindow {
    $hwnd = [Win32Focus]::FindWindowByTitle("Ninja Nexus")
    if ($hwnd -ne [IntPtr]::Zero) {
        [Win32Focus]::ShowWindow($hwnd, 9) | Out-Null
        [Win32Focus]::SetForegroundWindow($hwnd) | Out-Null
        Start-Sleep -Milliseconds 400
        return $true
    }
    return $false
}

function Save-Screenshot([string]$path) {
    Start-Sleep -Milliseconds 800
    Focus-NexusWindow | Out-Null
    Start-Sleep -Milliseconds 500

    $screen = [System.Windows.Forms.Screen]::PrimaryScreen
    $bounds = $screen.Bounds
    $full = New-Object System.Drawing.Bitmap $bounds.Width, $bounds.Height
    $g = [System.Drawing.Graphics]::FromImage($full)
    $g.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.Size)
    $g.Dispose()

    $cropH = [Math]::Min(280, $bounds.Height)
    $cropY = $bounds.Height - $cropH
    $crop = New-Object System.Drawing.Bitmap $bounds.Width, $cropH
    $cg = [System.Drawing.Graphics]::FromImage($crop)
    $cg.DrawImage($full, 0, 0, (New-Object System.Drawing.Rectangle 0, $cropY, $bounds.Width, $cropH), [System.Drawing.GraphicsUnit]::Pixel)
    $crop.Save($path, [System.Drawing.Imaging.ImageFormat]::Png)
    $cg.Dispose()
    $crop.Dispose()
    $full.Dispose()
    Write-Host "Saved $path"
}

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")
Start-Sleep -Seconds 3
Focus-NexusWindow | Out-Null
[System.Windows.Forms.SendKeys]::SendWait("user@nexus.local{TAB}NexusUser1!{ENTER}")
Start-Sleep -Seconds 5

Save-Screenshot (Join-Path $evidenceDir "dock-build-a.png")
Start-Sleep -Seconds 2
Save-Screenshot (Join-Path $evidenceDir "dock-build-b.png")

Write-Host "Dock evidence saved to $evidenceDir"
