[CmdletBinding()]
param(
  [string]$SmartCutCli = "build\windows-x64-debug\smartcut-cli.exe",
  [string]$InputFile = "build\m2-hdr10plus-variation.mkv",
  [int]$Frame = 3
)
$root = Split-Path -Parent $PSScriptRoot
$cli = [System.IO.Path]::GetFullPath((Join-Path $root $SmartCutCli))
$input = [System.IO.Path]::GetFullPath((Join-Path $root $InputFile))
if (-not (Test-Path -LiteralPath $cli -PathType Leaf)) { throw "Missing CLI: $cli" }
if (-not (Test-Path -LiteralPath $input -PathType Leaf)) { throw "Missing input: $input" }
$savedPath = $env:Path
$env:Path = (Join-Path $root "artifacts\m1\bin") + ";C:\msys642\ucrt64\bin;" + $env:Path
try {
  $source = (& $cli preview --input $input --frame $Frame --bgra) | ConvertFrom-Json
  $sdr = (& $cli preview --input $input --frame $Frame --tone-map-sdr) | ConvertFrom-Json
  if ($LASTEXITCODE -ne 0) { throw "Preview command failed" }
  $passed = ($source.display_color_mode -eq "source-bgra" -and !$source.tone_mapped -and $sdr.display_color_mode -eq "tone-mapped-sdr" -and $sdr.tone_mapped -and $source.frame_index -eq $sdr.frame_index -and $source.width -eq $sdr.width -and $source.height -eq $sdr.height -and $source.pixel_format -eq 28 -and $sdr.pixel_format -eq 28 -and $source.acknowledged -and $sdr.acknowledged)
  [ordered]@{ schema = "metadata-lab-m4-preview-gate-v1"; frame = $Frame; source_bgra = $source; tone_mapped_sdr = $sdr; passed = $passed } | ConvertTo-Json -Depth 8
  if (-not $passed) { exit 5 }
}
finally { $env:Path = $savedPath }
