param(
    [string]$DockerHost = "ssh://docker-gpu",
    [string]$SshTarget = "docker-gpu",
    [string]$EnvFile = "core/deploy/docker-gpu/training/1c-lora.env.example",
    [string]$ComposeFile = "core/deploy/docker-gpu/training/1c-lora.compose.yaml",
    [string]$RemoteArchive = "C:\ProgramData\LLM\model-chat-app.zip",
    [string]$RemoteAppDir = "Z:\LLM\model-chat-app",
    [switch]$PreflightOnly,
    [switch]$BuildConfigOnly,
    [switch]$SkipSyncApp
)

$ErrorActionPreference = "Stop"
. (Join-Path $PSScriptRoot "app_archive.ps1")

powershell -NoProfile -ExecutionPolicy Bypass -File scripts/check_gpu_host.ps1 -SshTarget $SshTarget

$env:PREFLIGHT_ONLY = if ($PreflightOnly) { "1" } else { "0" }

if (-not $SkipSyncApp) {
    $archive = New-AppArchive
    Test-AppArchive -Archive $archive
    Sync-AppDirectory -SshTarget $SshTarget -RemoteArchive $RemoteArchive -RemoteAppDir $RemoteAppDir -Archive $archive
}

if ($BuildConfigOnly) {
    docker --host $DockerHost compose `
        --env-file $EnvFile `
        -f $ComposeFile `
        config
    exit $LASTEXITCODE
}

docker --host $DockerHost compose `
    --env-file $EnvFile `
    -f $ComposeFile `
    up --abort-on-container-exit
