param(
    [string]$DockerHost = "ssh://docker-gpu.cin.su",
    [string]$SshTarget = "docker-gpu",
    [string]$ComposeFile = "core/deploy/docker-gpu/model-chat/compose.yaml",
    [string]$EnvFile = "core/deploy/docker-gpu/model-chat/.env.windows-z.example",
    [string]$RemoteArchive = "C:\ProgramData\LLM\model-chat-app.zip",
    [string]$RemoteAppDir = "Z:\LLM\model-chat-app",
    [switch]$ConfigOnly,
    [switch]$ArchiveOnly,
    [switch]$NoFirewall
)

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

if ($ConfigOnly) {
    docker --host $DockerHost compose --env-file $EnvFile -f $ComposeFile config | Out-Host
    exit 0
}

$archive = New-AppArchive
Test-AppArchive -Archive $archive
if ($ArchiveOnly) {
    Write-Host "Archive only: $archive"
    exit 0
}

docker --host $DockerHost compose --env-file $EnvFile -f $ComposeFile config | Out-Host
Sync-AppDirectory -SshTarget $SshTarget -RemoteArchive $RemoteArchive -RemoteAppDir $RemoteAppDir -Archive $archive

docker --host $DockerHost rm -f llm-model-chat-ui 2>$null | Out-Null
docker --host $DockerHost compose --env-file $EnvFile -f $ComposeFile up -d

if (-not $NoFirewall) {
    $firewallScript = @"
if (-not (Get-NetFirewallRule -DisplayName 'LLM Model Chat UI 8765' -ErrorAction SilentlyContinue)) {
    New-NetFirewallRule -DisplayName 'LLM Model Chat UI 8765' -Direction Inbound -Action Allow -Protocol TCP -LocalPort 8765 -Profile Any | Out-Null
}
"@
    Invoke-RemotePowerShell -SshTarget $SshTarget -Script $firewallScript
}

Write-Host "Model chat UI: http://192.168.220.91:8765/tools/model-chat/"
