[CmdletBinding()]
param(
  [string]$SmartCutCli = "build\windows-x64-debug\smartcut-cli.exe",
  [string]$InputFile = "build\m2-hdr10plus-variation.mkv",
  [string]$FrameIndexes = "0,1,2,3,4"
)
$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 {
  $results = @()
  $indexes = $FrameIndexes -split ',' | ForEach-Object { [int]$_.Trim() }
  foreach ($frame in $indexes) {
    $json = & $cli frame --input $input --frame $frame --max-decode ($frame + 1)
    if ($LASTEXITCODE -ne 0) { throw "Frame $frame decode failed" }
    $result = $json | ConvertFrom-Json
    if ($result.schema -ne "decoded-frame-v1" -or $result.frame_index -ne $frame -or $result.width -le 0 -or $result.height -le 0 -or $result.pixel_format -ne 62) { throw "Frame $frame returned invalid seek result" }
    $results += $result
  }
  [ordered]@{ schema = "metadata-lab-m4-seek-gate-v1"; input = $input; frames = $results; passed = $true } | ConvertTo-Json -Depth 8
}
finally { $env:Path = $savedPath }
