param(
    [switch]$SkipSmoke,
    [switch]$SkipApi
)

$ErrorActionPreference = "Stop"
$repoRoot = Split-Path -Parent $PSScriptRoot
$parityReport = Join-Path $repoRoot "logs\parity_harness_results.json"

Write-Host "migration-gate: repo root = $repoRoot"

Push-Location $repoRoot
try {
    Write-Host "migration-gate: building sidecar binary..."
    & "$repoRoot\scripts\dotnet-min.ps1" build Journal.Sidecar/Journal.Sidecar.csproj
    if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

    if (-not $SkipSmoke) {
        Write-Host "migration-gate: running csharp smoke tests..."
        & "$repoRoot\scripts\dotnet-min.ps1" run --project Journal.SmokeTests/Journal.SmokeTests.csproj
        if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
    }
    else {
        Write-Host "migration-gate: skipping smoke tests (--SkipSmoke)."
    }

    Write-Host "migration-gate: running parity harness + fixture matrix..."
    $testsDir = Join-Path $repoRoot "tests"
    if (Test-Path $testsDir) {
        $env:PARITY_HARNESS_REPORT = $parityReport
        & python -m unittest discover -s tests -p "test_parity_harness.py" -v
        if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
    }
    else {
        Write-Host "migration-gate: skipping parity harness — tests/ directory not found."
    }

    if (-not $SkipApi) {
        Write-Host "migration-gate: running API contract tests..."
        if (Test-Path $testsDir) {
            & python -m unittest discover -s tests -p "test_api_contract.py" -v
            if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
        }
        else {
            Write-Host "migration-gate: skipping API contract tests — tests/ directory not found."
        }
    }
    else {
        Write-Host "migration-gate: skipping API contract tests (--SkipApi)."
    }

    Write-Host "migration-gate: PASS"
    Write-Host "migration-gate: parity report => $parityReport"
}
finally {
    Pop-Location
}
