param(
    [switch]$FixtureOnly,
    [string]$FixturePath = (Join-Path $PSScriptRoot '..\ParkrobTrajplanner\PathSmoothing\Test\Fixtures\path-smoothing-fixtures.json'),
    [string]$OutputDirectory = (Join-Path $PSScriptRoot '..\obj\path_smoothing_reports')
)

$ErrorActionPreference = 'Stop'

$clumsyPilotRoot = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot '..')).Path
$allowedRoot = [IO.Path]::GetFullPath((Join-Path $clumsyPilotRoot 'obj\path_smoothing_reports'))
$resolvedOutputDirectory = [IO.Path]::GetFullPath($OutputDirectory)
$allowedPrefix = $allowedRoot.TrimEnd([IO.Path]::DirectorySeparatorChar, [IO.Path]::AltDirectorySeparatorChar) + [IO.Path]::DirectorySeparatorChar
if ($resolvedOutputDirectory -ne $allowedRoot -and -not $resolvedOutputDirectory.StartsWith($allowedPrefix, [StringComparison]::OrdinalIgnoreCase)) {
    throw "Report output must stay below $allowedRoot"
}

$projectPath = Join-Path $clumsyPilotRoot 'ClumsyPilot.csproj'
$hostProject = Join-Path $PSScriptRoot 'PathSmoothingPngVerificationHost\PathSmoothingPngVerificationHost.csproj'
$resolvedFixturePath = (Resolve-Path -LiteralPath $FixturePath).Path

& dotnet build $projectPath --no-restore
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

& dotnet run --project $hostProject --no-restore -- --export-fixtures $resolvedFixturePath $resolvedOutputDirectory
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

if (-not $FixtureOnly) {
    & dotnet run --project $hostProject --no-restore -- --export-end-to-end $resolvedOutputDirectory
    if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}

Write-Output "Path smoothing reports written below $resolvedOutputDirectory"
