$ErrorActionPreference = 'Stop'

$page = Join-Path $PSScriptRoot '..\ParkrobTrajplanner\Trajplanner_output\trajectory-planning-flow-demo.html'
if (-not (Test-Path -LiteralPath $page))
{
    throw "Missing demo page: $page"
}

$html = Get-Content -LiteralPath $page -Raw -Encoding UTF8
$required = @(
    '<meta charset="utf-8">',
    'data-stage="map"',
    'data-stage="coarse"',
    'data-stage="smooth"',
    'data-stage="em"',
    'data-stage="control"',
    'id="planning-map"',
    'id="timeline"',
    'coarse-path',
    'smooth-path',
    'EmTrajectory',
    'TrajectoryControlCommand',
    'mapSnapshotId',
    'referencePathId',
    'SignedLongitudinalVelocity',
    'RequestDirectionChange',
    'HoldBrake',
    'IsTrajectoryComplete',
    'function render()',
    'function controlCommandForPoint(',
    'addEventListener'
)

foreach ($token in $required)
{
    if (-not $html.Contains($token))
    {
        throw "Missing required demo contract: $token"
    }
}

foreach ($forbidden in @('http://', 'https://', 'fetch(', 'XMLHttpRequest', 'WebSocket'))
{
    if ($html.Contains($forbidden))
    {
        throw "Demo must remain offline: $forbidden"
    }
}

$readme = Get-Content -LiteralPath (Join-Path $PSScriptRoot '..\ParkrobTrajplanner\Trajplanner_output\README.md') -Raw -Encoding UTF8
if (-not $readme.Contains('trajectory-planning-flow-demo.html'))
{
    throw 'README must link to the trajectory-planning-flow demo.'
}

Write-Output 'Trajectory planning flow demo checks passed.'
