param(
    [string]$DockerHost = "ssh://docker-gpu",
    [string]$Image = "vllm/vllm-openai:v0.10.2",
    [string]$HostAppDir = "Z:/LLM/model-chat-app",
    [string]$HostModelsDir = "Z:/LLM/models",
    [string[]]$Model,
    [switch]$DryRun,
    [switch]$Detached,
    [string]$ContainerName = "llm-model-download"
)

$ErrorActionPreference = "Stop"

$argsList = @(
    "--host", $DockerHost,
    "run"
)

if ($Detached) {
    $argsList += @("-d", "--name", $ContainerName)
} else {
    $argsList += "--rm"
}

$argsList += @(
    "--entrypoint", "python3",
    "-e", "LLM_USE_RAW_MODEL_PATH=1",
    "-v", "${HostAppDir}:/app",
    "-v", "${HostModelsDir}:/models",
    "-w", "/app",
    $Image,
    "scripts/download_missing_hf_models.py"
)

if ($env:HF_TOKEN) {
    $argsList = @(
        "--host", $DockerHost,
        "run"
    )

    if ($Detached) {
        $argsList += @("-d", "--name", $ContainerName)
    } else {
        $argsList += "--rm"
    }

    $argsList += @(
        "--entrypoint", "python3",
        "-e", "LLM_USE_RAW_MODEL_PATH=1",
        "-e", "HF_TOKEN",
        "-v", "${HostAppDir}:/app",
        "-v", "${HostModelsDir}:/models",
        "-w", "/app",
        $Image,
        "scripts/download_missing_hf_models.py"
    )
}

foreach ($item in $Model) {
    $argsList += @("--model", $item)
}

if ($DryRun) {
    $argsList += "--dry-run"
}

Write-Host "Running GPU-host Hugging Face downloader..."
docker @argsList
