#requires -Version 7.2
[CmdletBinding()]
param(
    [string]$ScreenshotDirectory = (Join-Path $PSScriptRoot '..\screenshots')
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.Drawing

$Raw = Join-Path $ScreenshotDirectory 'raw'

function Save-Crop {
    param(
        [Parameter(Mandatory)][string]$Source,
        [Parameter(Mandatory)][string]$Destination,
        [Parameter(Mandatory)][int]$X,
        [Parameter(Mandatory)][int]$Y,
        [Parameter(Mandatory)][int]$Width,
        [Parameter(Mandatory)][int]$Height
    )
    $sourceImage = [System.Drawing.Bitmap]::FromFile($Source)
    try {
        if ($X -lt 0 -or $Y -lt 0 -or ($X + $Width) -gt $sourceImage.Width -or ($Y + $Height) -gt $sourceImage.Height) {
            throw "Crop is outside source bounds: $Source"
        }
        $target = [System.Drawing.Bitmap]::new($Width, $Height)
        $graphics = [System.Drawing.Graphics]::FromImage($target)
        try {
            $graphics.DrawImage($sourceImage, [System.Drawing.Rectangle]::new(0, 0, $Width, $Height), [System.Drawing.Rectangle]::new($X, $Y, $Width, $Height), [System.Drawing.GraphicsUnit]::Pixel)
            $target.Save($Destination, [System.Drawing.Imaging.ImageFormat]::Png)
        }
        finally {
            $graphics.Dispose()
            $target.Dispose()
        }
    }
    finally { $sourceImage.Dispose() }
}

function Add-To-Composite {
    param(
        [Parameter(Mandatory)][System.Drawing.Graphics]$Graphics,
        [Parameter(Mandatory)][string]$Source,
        [Parameter(Mandatory)][System.Drawing.Rectangle]$SourceRectangle,
        [Parameter(Mandatory)][System.Drawing.Rectangle]$DestinationRectangle
    )
    $image = [System.Drawing.Bitmap]::FromFile($Source)
    try {
        $Graphics.DrawImage($image, $DestinationRectangle, $SourceRectangle, [System.Drawing.GraphicsUnit]::Pixel)
    }
    finally { $image.Dispose() }
}

Save-Crop -Source (Join-Path $Raw '02-network-manager-scope-and-access-full.png') -Destination (Join-Path $ScreenshotDirectory '02-network-manager-scope-and-access.png') -X 225 -Y 55 -Width 1823 -Height 480
Save-Crop -Source (Join-Path $Raw '03-ipam-root-and-child-pools-full.png') -Destination (Join-Path $ScreenshotDirectory '03-root-and-child-ipam-pools.png') -X 225 -Y 55 -Width 1823 -Height 800
Save-Crop -Source (Join-Path $Raw '05-ipam-vnet-allocations-and-reservation-full.png') -Destination (Join-Path $ScreenshotDirectory '04-ipam-usage-and-reserved-cidr.png') -X 225 -Y 55 -Width 1823 -Height 872
Save-Crop -Source (Join-Path $Raw '07-intentional-edge-tag-typo-full.png') -Destination (Join-Path $ScreenshotDirectory '07-intentional-edge-tag-typo.png') -X 225 -Y 55 -Width 1823 -Height 600
Save-Crop -Source (Join-Path $Raw '08-dynamic-network-data-policy-full.png') -Destination (Join-Path $ScreenshotDirectory '08-dynamic-network-data-policy.png') -X 225 -Y 55 -Width 1050 -Height 520
Save-Crop -Source (Join-Path $Raw '09-initial-three-member-network-group-full.png') -Destination (Join-Path $ScreenshotDirectory '09-initial-three-member-network-group.png') -X 225 -Y 55 -Width 1823 -Height 350
Save-Crop -Source (Join-Path $Raw '10-regional-mesh-four-connected-full.png') -Destination (Join-Path $ScreenshotDirectory '10-regional-mesh-configuration.png') -X 225 -Y 55 -Width 1823 -Height 380
Save-Crop -Source (Join-Path $Raw '11-core-connectedgroup-effective-routes-full.png') -Destination (Join-Path $ScreenshotDirectory '11-connectedgroup-effective-routes.png') -X 225 -Y 55 -Width 1400 -Height 570

$canvas13 = [System.Drawing.Bitmap]::new(2048, 927)
$graphics13 = [System.Drawing.Graphics]::FromImage($canvas13)
try {
    $graphics13.Clear([System.Drawing.Color]::FromArgb(9, 14, 22))
    Add-To-Composite -Graphics $graphics13 -Source (Join-Path $Raw '13-corrected-edge-tag-full.png') -SourceRectangle ([System.Drawing.Rectangle]::new(225, 55, 1823, 600)) -DestinationRectangle ([System.Drawing.Rectangle]::new(24, 18, 2000, 530))
    Add-To-Composite -Graphics $graphics13 -Source (Join-Path $Raw '13-four-member-network-group-full.png') -SourceRectangle ([System.Drawing.Rectangle]::new(225, 55, 1823, 350)) -DestinationRectangle ([System.Drawing.Rectangle]::new(24, 572, 2000, 335))
    $canvas13.Save((Join-Path $ScreenshotDirectory '13-corrected-tag-and-four-member-group.png'), [System.Drawing.Imaging.ImageFormat]::Png)
}
finally {
    $graphics13.Dispose()
    $canvas13.Dispose()
}

$canvas15 = [System.Drawing.Bitmap]::new(2048, 927)
$graphics15 = [System.Drawing.Graphics]::FromImage($canvas15)
try {
    $graphics15.Clear([System.Drawing.Color]::FromArgb(9, 14, 22))
    Add-To-Composite -Graphics $graphics15 -Source (Join-Path $Raw '15-nsg-allows-both-ports-full.png') -SourceRectangle ([System.Drawing.Rectangle]::new(225, 350, 1823, 390)) -DestinationRectangle ([System.Drawing.Rectangle]::new(24, 18, 2000, 420))
    Add-To-Composite -Graphics $graphics15 -Source (Join-Path $Raw '15-security-admin-applied-full.png') -SourceRectangle ([System.Drawing.Rectangle]::new(225, 55, 1823, 510)) -DestinationRectangle ([System.Drawing.Rectangle]::new(24, 480, 2000, 427))
    $canvas15.Save((Join-Path $ScreenshotDirectory '15-nsg-allow-and-security-admin-deny.png'), [System.Drawing.Imaging.ImageFormat]::Png)
}
finally {
    $graphics15.Dispose()
    $canvas15.Dispose()
}

Write-Host 'Sanitized Portal screenshots created. Global navigation, account header, IDs, and unrelated resource details are excluded.'
