param([string]$MapRoot = (Join-Path $PSScriptRoot '..\ParkrobTrajplanner\Map'))

$ErrorActionPreference = 'Stop'

function Assert-Contains([string]$Content, [string]$Expected, [string]$Message) {
    if (-not $Content.Contains($Expected)) { throw "$Message Missing=$Expected" }
}

function Assert-DocumentationCount([string]$RelativePath, [int]$MinimumCount) {
    $path = Join-Path $MapRoot $RelativePath
    $content = Get-Content -LiteralPath $path -Raw
    $count = [regex]::Matches($content, '/// <summary>').Count
    if ($count -lt $MinimumCount) { throw "Insufficient public API documentation in $RelativePath ExpectedAtLeast=$MinimumCount Actual=$count" }
}

$readmePath = Join-Path $MapRoot 'README.md'
if (-not (Test-Path -LiteralPath $readmePath)) { throw "Map README is required: $readmePath" }
$readme = Get-Content -LiteralPath $readmePath -Raw
Assert-Contains $readme 'PlanningMapFactory' 'README must identify the public creation facade.'
Assert-Contains $readme 'IMapObstacleSource' 'README must explain unified obstacle sources.'
Assert-Contains $readme 'PlanningGridMap' 'README must explain the planning snapshot output.'
Assert-Contains $readme 'SourceVersion' 'README must explain cache invalidation versions.'
Assert-Contains $readme 'PlanningMapBuildStatus' 'README must explain explicit map build termination states.'
Assert-Contains $readme 'PNG' 'README must explain debug image output.'
Assert-Contains $readme 'TrapMap' 'README must describe the legacy-map boundary.'

Assert-DocumentationCount 'PlanningMapFactory.cs' 2
Assert-DocumentationCount 'PlanningMapRequest.cs' 5
Assert-DocumentationCount 'PlanningMapBuildResult.cs' 9
Assert-DocumentationCount 'Core\MapBuildRequest.cs' 4
Assert-DocumentationCount 'Core\EnvironmentMapBuildResult.cs' 7
Assert-DocumentationCount 'Core\MapBoundsMm.cs' 12
Assert-DocumentationCount 'Core\EnvironmentGridMap.cs' 13
Assert-DocumentationCount 'Core\EnvironmentMapBuilder.cs' 2
Assert-DocumentationCount 'Obstacles\IMapObstacle.cs' 2
Assert-DocumentationCount 'Obstacles\CircleObstacle.cs' 6
Assert-DocumentationCount 'Obstacles\AxisAlignedRectangleObstacle.cs' 7
Assert-DocumentationCount 'Obstacles\MapObstacleRasterizer.cs' 2
Assert-DocumentationCount 'Sources\IMapObstacleSource.cs' 5
Assert-DocumentationCount 'Sources\ManualObstacleSource.cs' 6
Assert-DocumentationCount 'Sources\TwoLegProjectionInput.cs' 13
Assert-DocumentationCount 'Sources\TwoLegObstacleSource.cs' 6
Assert-DocumentationCount 'Sources\TwoLegObstacleProjector.cs' 2
Assert-DocumentationCount 'Sources\ObstacleProjectionResult.cs' 8
Assert-DocumentationCount 'Sources\ObstacleSourceStatus.cs' 5
Assert-DocumentationCount 'Planning\PlanningGridMap.cs' 15
Assert-DocumentationCount 'Planning\PlanningMapAdapter.cs' 2
Assert-DocumentationCount 'Planning\ObstacleDistanceField.cs' 2
Assert-DocumentationCount 'Planning\EuclideanDistanceTransform.cs' 2
Assert-DocumentationCount 'Planning\PlanningMapCache.cs' 5
Assert-DocumentationCount 'Test\MovementTest.MapTest.cs' 3
Assert-DocumentationCount 'Test\Visualization\PlanningMapImageExportRequest.cs' 3
Assert-DocumentationCount 'Test\Visualization\PlanningMapImageExportResult.cs' 8
Assert-DocumentationCount 'Test\Visualization\PlanningMapImageExporter.cs' 6
Assert-DocumentationCount 'Test\Visualization\PlanningMapImageRenderer.cs' 2
Assert-DocumentationCount 'Test\Visualization\ValidatedPngWriter.cs' 2

Write-Output 'Planning map documentation checks passed.'
