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

$ErrorActionPreference = 'Stop'

function Assert-True($Actual, [string]$Message) {
    if (-not $Actual) { throw $Message }
}

function Assert-FileExists([string]$RelativePath) {
    Assert-True (Test-Path -LiteralPath (Join-Path $PathSmoothingRoot $RelativePath) -PathType Leaf) "Missing required file: $RelativePath"
}

function Assert-DirectoryExists([string]$RelativePath) {
    Assert-True (Test-Path -LiteralPath (Join-Path $PathSmoothingRoot $RelativePath) -PathType Container) "Missing required directory: $RelativePath"
}

function Assert-PathAbsent([string]$RelativePath) {
    Assert-True (-not (Test-Path -LiteralPath (Join-Path $PathSmoothingRoot $RelativePath))) "Removed legacy path still exists: $RelativePath"
}

Assert-DirectoryExists 'Contracts'
Assert-DirectoryExists 'Facade'
Assert-DirectoryExists 'LocalG2'
Assert-DirectoryExists 'Processing'
Assert-DirectoryExists 'Validation'
Assert-DirectoryExists 'Output'
Assert-DirectoryExists 'Output\Comparison'
Assert-DirectoryExists 'Output\Visualization'
Assert-DirectoryExists 'Test'
Assert-FileExists 'README.md'

foreach ($legacyPath in @(
    'Algorithms',
    'Comparison',
    'Visualization',
    'Contracts\CubicBSplineOptions.cs',
    'Contracts\LocalCubicBezierOptions.cs',
    'Contracts\PiecewiseQuinticOptions.cs',
    'Output\Comparison\SmoothingMethodRanker.cs'
)) {
    Assert-PathAbsent $legacyPath
}

$methodSource = Get-Content -Raw -Encoding UTF8 (Join-Path $PathSmoothingRoot 'Contracts\SmoothingMethod.cs')
Assert-True ($methodSource -match 'LocalG2Quintic') 'SmoothingMethod must retain LocalG2Quintic.'
foreach ($legacyMethod in @('CubicBSpline', 'LocalCubicBezier', 'PiecewiseQuintic')) {
    Assert-True (-not $methodSource.Contains($legacyMethod)) "SmoothingMethod must not retain $legacyMethod."
}

$configurationSource = Get-Content -Raw -Encoding UTF8 (Join-Path $PathSmoothingRoot 'Contracts\PathSmoothingConfiguration.cs')
foreach ($legacyConfiguration in @('CubicBSpline', 'LocalCubicBezier', 'PiecewiseQuintic', 'SmoothingStrength', 'RetryStrengthScales', 'AllowFallbackToCoarsePath')) {
    Assert-True (-not $configurationSource.Contains($legacyConfiguration)) "PathSmoothingConfiguration must not retain $legacyConfiguration."
}
Assert-True ($configurationSource.Contains('MinimumClearanceReserveMeters = 0d;')) 'The requested 0 m clearance-reserve default must remain.'

$readme = Get-Content -Raw -Encoding UTF8 (Join-Path $PathSmoothingRoot 'README.md')
foreach ($heading in @(
    '# PathSmoothing',
    'Module Overview',
    'File Structure',
    'Smoothing Data Flow',
    'Result Status and Publication Rules',
    'Coordinates and Units',
    'Minimal Call Example',
    'Detailed Usage Guide',
    'Fixture Reports and Visualization',
    'Common Errors',
    'First-Version Limits'
)) {
    Assert-True ($readme.Contains($heading)) "README is missing required section: $heading"
}

Write-Output 'PathSmoothing LocalG2-only layout checks passed.'
