param(
    [Parameter(Mandatory = $true)]
    [ValidateSet("translation", "audio", "video", "image")]
    [string]$Plugin,
    [string]$DockerHost = "ssh://docker-gpu",
    [string]$SshTarget = "docker-gpu",
    [string]$RemoteArchive = "C:\ProgramData\LLM\model-chat-app.zip",
    [string]$RemoteAppDir = "Z:\LLM\model-chat-app",
    [switch]$ConfigOnly,
    [switch]$Pull,
    [switch]$NoSyncApp,
    [switch]$NoFirewall
)

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

$composeFile = "core/deploy/docker-gpu/transformers/$Plugin.compose.yaml"
$envFile = "core/deploy/docker-gpu/transformers/$Plugin.env.example"
$ports = @{
    translation = 8010
    audio = 8020
    video = 8030
    image = 8040
}

if (-not (Test-Path -LiteralPath $composeFile)) {
    throw "Compose file not found: $composeFile"
}

if (-not (Test-Path -LiteralPath $envFile)) {
    throw "Env file not found: $envFile"
}

$baseArgs = @(
    "--host", $DockerHost,
    "compose",
    "--env-file", $envFile,
    "-f", $composeFile
)

docker @baseArgs config | Out-Host
if ($ConfigOnly) {
    exit 0
}

if ($Pull) {
    docker @baseArgs pull
    if ($LASTEXITCODE -ne 0) {
        exit $LASTEXITCODE
    }
}

if (-not $NoSyncApp) {
    Sync-AppDirectory -SshTarget $SshTarget -RemoteArchive $RemoteArchive -RemoteAppDir $RemoteAppDir
}

docker @baseArgs up -d
if ($LASTEXITCODE -ne 0) {
    exit $LASTEXITCODE
}

if (-not $NoFirewall) {
    $port = $ports[$Plugin]
    $ruleName = "LLM Transformers $Plugin $port"
    $firewallScript = @"
if (-not (Get-NetFirewallRule -DisplayName '$ruleName' -ErrorAction SilentlyContinue)) {
    New-NetFirewallRule -DisplayName '$ruleName' -Direction Inbound -Action Allow -Protocol TCP -LocalPort $port -Profile Any | Out-Null
}
"@
    Invoke-RemotePowerShell -SshTarget $SshTarget -Script $firewallScript
}

exit $LASTEXITCODE
