[CmdletBinding()]
param(
    [Parameter(Mandatory = $true)]
    [string]$EvaluationPack,

    [Parameter(Mandatory = $true)]
    [string]$ValidFovRoot,

    [Parameter(Mandatory = $true)]
    [string]$RunnerPath,

    [Parameter(Mandatory = $true)]
    [string]$BaselineRunnerPath,

    [string]$RuntimeRoot = "D:\NDC_MISSIONCORE\runtime"
)

$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"

function Assert-LastExitCode {
    param([string]$Operation)
    if ($LASTEXITCODE -ne 0) {
        throw "$Operation failed with exit code $LASTEXITCODE"
    }
}

function Assert-RegularFile {
    param([string]$Path, [string]$Label)
    $item = Get-Item -LiteralPath $Path -Force
    if (-not $item.PSIsContainer -and -not ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) {
        return $item.FullName
    }
    throw "$Label is not a regular file"
}

function Assert-Directory {
    param([string]$Path, [string]$Label)
    $item = Get-Item -LiteralPath $Path -Force
    if ($item.PSIsContainer -and -not ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) {
        return $item.FullName
    }
    throw "$Label is not a real directory"
}

function Convert-ToDockerPath {
    param([string]$Path)
    return $Path.Replace("\", "/")
}

$packRoot = Assert-Directory (Resolve-Path -LiteralPath $EvaluationPack).Path "Evaluation pack"
$validFov = Assert-Directory (Resolve-Path -LiteralPath $ValidFovRoot).Path "Valid-FOV root"
$runner = Assert-RegularFile (Resolve-Path -LiteralPath $RunnerPath).Path "Prelabel runner"
$baselineRunner = Assert-RegularFile (Resolve-Path -LiteralPath $BaselineRunnerPath).Path "Baseline runner"
if ((Split-Path $runner -Parent) -ne (Split-Path $baselineRunner -Parent)) {
    throw "Prelabel and baseline runners must share one read-only mount"
}
$runtime = Assert-Directory (Resolve-Path -LiteralPath $RuntimeRoot).Path "Runtime root"
$pack = Get-Content -LiteralPath (Join-Path $packRoot "manifest.json") -Raw | ConvertFrom-Json
if (
    $pack.schema_version -ne "missioncore.perception-evaluation-pack/v1" -or
    $pack.generation_id -ne (Split-Path $packRoot -Leaf) -or
    $pack.identity.preprocessing_profile -ne "fixed-valid-fov-fill/v1"
) {
    throw "Evaluation pack manifest is incompatible"
}
$null = Assert-RegularFile (Join-Path $validFov "manifest.json") "Valid-FOV manifest"
$null = Assert-RegularFile (Join-Path $validFov "mask.png") "Valid-FOV mask"

$cacheRoot = Assert-Directory (Join-Path $runtime "cache\perception-p0-models-v1") "Model cache"
$torchEnvironment = Assert-Directory (Join-Path $runtime "derived\perception-p0-env-v1") "Torch environment"
$transformersEnvironment = Assert-Directory (Join-Path $runtime "derived\perception-p0-transformers4576-v1") "Transformers environment"
$runnerRoot = Split-Path $runner -Parent
$runnerName = Split-Path $runner -Leaf
$derivedRoot = Join-Path $runtime "derived\evaluation-prelabels"
$null = New-Item -ItemType Directory -Path $derivedRoot -Force
$runToken = [Guid]::NewGuid().ToString("N")
$publishRoot = Join-Path $derivedRoot (".{0}-{1}.publish" -f $pack.generation_id, $runToken)
$stagingRoot = Join-Path $publishRoot "output"
$null = New-Item -ItemType Directory -Path $publishRoot

try {
    $dockerArgs = @(
        "run", "--rm", "--gpus", "all", "--network", "none",
        "--security-opt", "no-new-privileges:true", "--cap-drop", "ALL",
        "--shm-size", "2g",
        "-e", "PYTHONPATH=/opt/transformers:/opt/env",
        "-e", "TORCH_HOME=/cache/torch",
        "-e", "HF_HOME=/cache/huggingface",
        "-e", "HF_HUB_OFFLINE=1",
        "-e", "TRANSFORMERS_OFFLINE=1",
        "-v", ((Convert-ToDockerPath $packRoot) + ":/evaluation-pack:ro"),
        "-v", ((Convert-ToDockerPath $validFov) + ":/valid-fov:ro"),
        "-v", ((Convert-ToDockerPath $publishRoot) + ":/publish:rw"),
        "-v", ((Convert-ToDockerPath $cacheRoot) + ":/cache:rw"),
        "-v", ((Convert-ToDockerPath $torchEnvironment) + ":/opt/env:ro"),
        "-v", ((Convert-ToDockerPath $transformersEnvironment) + ":/opt/transformers:ro"),
        "-v", ((Convert-ToDockerPath $runnerRoot) + ":/runner:ro"),
        "--entrypoint", "python3",
        "nvcr.io/nvidia/tritonserver:26.06-py3",
        ("/runner/{0}" -f $runnerName),
        "--evaluation-pack", "/evaluation-pack",
        "--valid-fov-root", "/valid-fov",
        "--output", "/publish/output",
        "--cache", "/cache",
        "--telemetry-interval-seconds", "1"
    )
    Write-Output ("PHASE=e2-prelabels-start PACK={0}" -f $pack.generation_id)
    & docker @dockerArgs
    Assert-LastExitCode "E2 prelabel generation"

    $result = Get-Content -LiteralPath (Join-Path $stagingRoot "result.json") -Raw | ConvertFrom-Json
    if (
        $result.schema_version -ne "missioncore.perception-evaluation-prelabels/v1" -or
        $result.result_id -notmatch "^evaluation-prelabels-[a-f0-9]{64}$" -or
        $result.identity.evaluation_pack_id -ne $pack.generation_id
    ) {
        throw "E2 prelabel result manifest is incompatible"
    }
    $finalRoot = Join-Path $derivedRoot ([string]$result.result_id)
    if (Test-Path -LiteralPath $finalRoot) {
        throw "An immutable E2 prelabel result with the same identity already exists: $finalRoot"
    }
    Move-Item -LiteralPath $stagingRoot -Destination $finalRoot
    Remove-Item -LiteralPath $publishRoot -Force
    Write-Output ("RESULT_ROOT={0}" -f $finalRoot)
    Write-Output ("RESULT_ID={0}" -f $result.result_id)
}
finally {
    if (Test-Path -LiteralPath $publishRoot) {
        Remove-Item -LiteralPath $publishRoot -Recurse -Force
    }
}
