param(
    [string]$ImageRootPath = "",
    [string]$Filter = "",
    [string]$OutputPath = "",
    [int]$Limit = 0,
    [int]$OnAirDelayMs = 900,
    [int]$BetweenDelayMs = 250,
    [switch]$IncludeVideoWall
)

$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot "..\..\..\..\..")).Path
$probeProject = Join-Path $repoRoot "tools\KarismaTcpProbe\KarismaTcpProbe.csproj"
$fixedImageRootPath = "D:\Elect2026\T3_Cut"

if ([string]::IsNullOrWhiteSpace($OutputPath))
{
    $timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
    $OutputPath = Join-Path $repoRoot "artifacts\cut-design-debugger\live-cut-validation\$timestamp"
}

$commandArgs = @(
    "run",
    "--project", $probeProject,
    "--",
    "--validate-live-cuts",
    "--output", $OutputPath,
    "--onair-delay-ms", $OnAirDelayMs.ToString(),
    "--between-delay-ms", $BetweenDelayMs.ToString()
)

if (-not [string]::IsNullOrWhiteSpace($Filter))
{
    $commandArgs += @("--filter", $Filter)
}

if ($Limit -gt 0)
{
    $commandArgs += @("--limit", $Limit.ToString())
}

if ($IncludeVideoWall)
{
    $commandArgs += "--include-videowall"
}

Write-Host "Running live-cut validation..."
Write-Host "Repo Root : $repoRoot"
Write-Host "Image Root: $fixedImageRootPath"
Write-Host "Output    : $OutputPath"
if (-not [string]::IsNullOrWhiteSpace($ImageRootPath))
{
    Write-Host "ImageRootPath parameter ignored; fixed root is used."
}
if (-not [string]::IsNullOrWhiteSpace($Filter))
{
    Write-Host "Filter    : $Filter"
}

& dotnet @commandArgs
if ($LASTEXITCODE -ne 0)
{
    exit $LASTEXITCODE
}

$summaryPath = Join-Path $OutputPath "summary.md"
Write-Host "Validation report: $summaryPath"
