[CmdletBinding()]
param()

$ErrorActionPreference = 'Stop'

$root = $PSScriptRoot
$runDir = Join-Path $root 'training'
$stdoutPath = Join-Path $runDir 'scheduled-training.stdout.log'
$stderrPath = Join-Path $runDir 'scheduled-training.stderr.log'
$exitPath = Join-Path $runDir 'scheduled-training-exit.json'
New-Item -ItemType Directory -Force -Path $runDir | Out-Null
if (Test-Path -LiteralPath $exitPath) {
    Remove-Item -LiteralPath $exitPath -Force
}

$exitCode = 1
try {
    $arguments = @(
        '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass',
        '-File', (Join-Path $root 'train_piper_windows.ps1'),
        '-DatasetDir', (Join-Path $root 'dataset'),
        '-OutputDir', $runDir,
        '-CacheDir', (Join-Path $root 'smoke\cache'),
        '-PythonExe', (Join-Path $root '.venv\Scripts\python.exe'),
        '-BatchSize', '16', '-NumWorkers', '0', '-MaxEpochs', '2000'
    )
    $process = Start-Process -FilePath 'powershell.exe' `
        -ArgumentList $arguments `
        -RedirectStandardOutput $stdoutPath `
        -RedirectStandardError $stderrPath `
        -WindowStyle Hidden `
        -Wait `
        -PassThru
    $exitCode = $process.ExitCode
} catch {
    $_ | Out-String | Add-Content -LiteralPath $stderrPath -Encoding utf8
    $exitCode = 1
} finally {
    [ordered]@{
        exit_code = $exitCode
        finished_utc = (Get-Date).ToUniversalTime().ToString('o')
    } | ConvertTo-Json | Set-Content -LiteralPath $exitPath -Encoding utf8
}

exit $exitCode
