param(
    [string]$DockerHost = "ssh://docker-gpu",
    [string]$Image = "python:3.11-slim",
    [string]$HostModelsDir = "Z:/LLM/models",
    [string]$HostToolsDir = "Z:/LLM/tools",
    [string]$AdapterDir = "/models/adapters/1c/qwen3-coder-30b-a3b-1c-lora-v1",
    [string]$BaseModelDir = "/models/base/qwen3-coder-30b-a3b-instruct",
    [string]$OutputFile = "/models/adapters/1c/qwen3-coder-30b-a3b-1c-lora-v1.gguf",
    [ValidateSet("f16", "bf16")]
    [string]$OutType = "f16",
    [switch]$SkipClone,
    [switch]$Detached,
    [string]$ContainerName = "llm-convert-1c-lora-gguf"
)

$ErrorActionPreference = "Stop"

$script = @'
set -euo pipefail

export DEBIAN_FRONTEND=noninteractive

if [ ! -d /models ]; then
  echo "/models mount is missing" >&2
  exit 1
fi

mkdir -p /tools

if [ "${SKIP_CLONE:-0}" != "1" ]; then
  if [ ! -d /tools/llama.cpp/.git ]; then
    rm -rf /tools/llama.cpp
    git clone --depth 1 https://github.com/ggml-org/llama.cpp.git /tools/llama.cpp
  else
    git -C /tools/llama.cpp pull --ff-only
  fi
fi

python -m pip install --no-cache-dir --upgrade pip
python -m pip install --no-cache-dir -r /tools/llama.cpp/requirements.txt

python /tools/llama.cpp/convert_lora_to_gguf.py \
  --base "$BASE_MODEL_DIR" \
  --outfile "$OUTPUT_FILE" \
  --outtype "$OUTTYPE" \
  "$ADAPTER_DIR"
'@

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

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

$dockerArgs += @(
    "--init",
    "-v", "${HostModelsDir}:/models",
    "-v", "${HostToolsDir}:/tools",
    "-e", "ADAPTER_DIR=$AdapterDir",
    "-e", "BASE_MODEL_DIR=$BaseModelDir",
    "-e", "OUTPUT_FILE=$OutputFile",
    "-e", "OUTTYPE=$OutType",
    "-e", ("SKIP_CLONE=" + ($(if ($SkipClone) { "1" } else { "0" }))),
    "--entrypoint", "bash",
    $Image,
    "-lc",
    "apt-get update && apt-get install -y --no-install-recommends git build-essential && " + $script
)

docker @dockerArgs
