param(
    [Parameter(Mandatory = $true)]
    [string]$CardId,
    [Parameter(Mandatory = $true)]
    [string]$LocalDir,
[string[]]$AllowFile,
    [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]$ChunkSize = "256mb",
    [int]$Retries = 40,
    [int]$TimeoutSeconds = 180,
    [switch]$Detached,
    [string]$ContainerName = "llm-hf-range-download"
)

$ErrorActionPreference = "Stop"

$dockerArgs = @(
    "--host", $DockerHost,
    "run",
    "--init"
)

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

$dockerArgs += @(
    "--entrypoint", "python3",
    "-e", "LLM_USE_RAW_MODEL_PATH=1",
    "-e", "PYTHONUNBUFFERED=1",
    "-v", "${HostAppDir}:/app",
    "-v", "${HostModelsDir}:/models",
    "-w", "/app",
    $Image,
    "scripts/download_hf_range.py",
    $CardId,
    "--local-dir", $LocalDir,
    "--chunk-size", $ChunkSize,
    "--retries", "$Retries",
    "--timeout", "$TimeoutSeconds"
)

$allowFilesNormalized = @()
foreach ($entry in ($AllowFile | Where-Object { $_ })) {
    $allowFilesNormalized += ($entry -split "," | ForEach-Object { $_.Trim() } | Where-Object { $_ })
}

foreach ($file in $allowFilesNormalized) {
    $dockerArgs += @("--allow-file", $file)
}

docker @dockerArgs
