Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

# Resolve Python executable: prefer workspace venv for deterministic behavior.
$venvPython = Join-Path $PSScriptRoot ".venv\Scripts\python.exe"
if (Test-Path -LiteralPath $venvPython) {
  $pythonExe = $venvPython
} else {
  $pythonExe = "python"
}

Write-Host "[INFO] Using Python: $pythonExe"

# Clean generated outputs to validate a fresh rebuild.
$targets = @(
  (Join-Path $PSScriptRoot "unicode_reports"),
  (Join-Path $PSScriptRoot "normalized_outputs_all_with_json")
)

foreach ($t in $targets) {
  if (Test-Path -LiteralPath $t) {
    Remove-Item -LiteralPath $t -Recurse -Force
    Write-Host "[OK] Removed: $t"
  }
}

Push-Location $PSScriptRoot
try {
  & $pythonExe "ornamentation_scanner.py" --in-dir "." --out-dir "unicode_reports"
  if ($LASTEXITCODE -ne 0) { throw "Scanner failed with exit code $LASTEXITCODE" }

  & $pythonExe "normalization_engine.py" --in-dir "." --out-dir "normalized_outputs_all_with_json"
  if ($LASTEXITCODE -ne 0) { throw "Normalizer failed with exit code $LASTEXITCODE" }

  $bad1 = Get-ChildItem -LiteralPath "normalized_outputs_all_with_json" -Recurse -File |
    Where-Object { $_.Name -match "__normalized|__unicode_report" }
  $bad2 = Get-ChildItem -LiteralPath "unicode_reports" -Recurse -File |
    Where-Object { $_.Name -match "__normalized|__unicode_report" }

  if (($bad1 | Measure-Object).Count -gt 0 -or ($bad2 | Measure-Object).Count -gt 0) {
    throw "Naming verification failed: found legacy double-underscore outputs."
  }

  # Determinism check for text outputs: rerun and compare SHA256 hashes.
  $tracked = @()
  $tracked += Get-ChildItem -LiteralPath "normalized_outputs_all_with_json" -Recurse -File |
    Where-Object { $_.Extension -in ".csv", ".json" }
  $tracked += Get-ChildItem -LiteralPath "unicode_reports" -Recurse -File |
    Where-Object { $_.Extension -in ".json", ".txt" }

  $before = @{}
  foreach ($f in $tracked) {
    $before[$f.FullName] = (Get-FileHash -LiteralPath $f.FullName -Algorithm SHA256).Hash
  }

  & $pythonExe "ornamentation_scanner.py" --in-dir "." --out-dir "unicode_reports" | Out-Null
  if ($LASTEXITCODE -ne 0) { throw "Scanner rerun failed with exit code $LASTEXITCODE" }

  & $pythonExe "normalization_engine.py" --in-dir "." --out-dir "normalized_outputs_all_with_json" | Out-Null
  if ($LASTEXITCODE -ne 0) { throw "Normalizer rerun failed with exit code $LASTEXITCODE" }

  $changed = 0
  foreach ($k in $before.Keys) {
    if (-not (Test-Path -LiteralPath $k)) {
      $changed++
      continue
    }
    $afterHash = (Get-FileHash -LiteralPath $k -Algorithm SHA256).Hash
    if ($afterHash -ne $before[$k]) {
      $changed++
    }
  }

  if ($changed -ne 0) {
    throw "Determinism check failed: $changed tracked text outputs changed across rerun."
  }

  Write-Host "[OK] Reproducibility verification passed."
  Write-Host "[OK] Fresh rebuild succeeded, naming contract holds, and text outputs are deterministic."
}
finally {
  Pop-Location
}
