#!/usr/bin/env bash
set -euo pipefail

IMAGE_NAME="${PTY_AGENT_IMAGE:-pty-agent:debian}"
CONTAINER_NAME="${PTY_AGENT_CONTAINER:-pty-agent-run}"
CRATE_VERSION="${PTY_AGENT_VERSION:-}"
INSTALL_SOURCE="${PTY_AGENT_INSTALL_SOURCE:-crates}"
PLATFORM="${PTY_AGENT_PLATFORM:-}"
PROJECT_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
DOCKERFILE="$PROJECT_ROOT/docker/Dockerfile"
NO_CACHE=()

usage() {
  cat <<'EOF'
Usage: run-pty-agent-docker.sh [OPTIONS] "task"

Build/run the Debian image and start pty-agent in a clean container.

Options:
  --local     Mount this checkout at /src/pty-agent and install from local path
  --rebuild   Build with --no-cache
  -h, --help  Show this help

Environment:
  OPENAI_API_KEY      Passed through to pty-agent
  OPENAI_BASE_URL     Optional OpenAI-compatible endpoint
  PTY_AGENT_IMAGE     Docker image tag, default: pty-agent:debian
  PTY_AGENT_CONTAINER Docker container name, default: pty-agent-run
  PTY_AGENT_PLATFORM  Optional Docker platform override
  PTY_AGENT_VERSION   Optional pty-agent crates.io version
EOF
}

if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
  usage
  exit 0
fi

if ! command -v docker >/dev/null 2>&1; then
  echo "docker is required but was not found on PATH" >&2
  exit 1
fi

if ! docker info >/dev/null 2>&1; then
  cat >&2 <<'EOF'
docker is installed, but the Docker daemon is not reachable.

Start Docker Desktop, or start your Docker daemon, then rerun this script.
EOF
  exit 1
fi

while [ $# -gt 0 ]; do
  case "$1" in
    --local)
      INSTALL_SOURCE="local"
      shift
      ;;
    --rebuild)
      NO_CACHE=(--no-cache)
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    --)
      shift
      break
      ;;
    *)
      break
      ;;
  esac
done

BUILD_ARGS=()
RUN_ARGS=()

if [ -n "$PLATFORM" ]; then
  BUILD_ARGS+=(--platform "$PLATFORM")
  RUN_ARGS+=(--platform "$PLATFORM")
fi

ENV_ARGS=(-e SHELL=/bin/bash)

if [ -n "${OPENAI_API_KEY:-}" ]; then
  ENV_ARGS+=(-e OPENAI_API_KEY="$OPENAI_API_KEY")
fi

if [ -n "${OPENAI_BASE_URL:-}" ]; then
  ENV_ARGS+=(-e OPENAI_BASE_URL="$OPENAI_BASE_URL")
fi

if [ -n "${PTY_AGENT_MODEL:-}" ]; then
  ENV_ARGS+=(-e PTY_AGENT_MODEL="$PTY_AGENT_MODEL")
fi

if [ "$INSTALL_SOURCE" = "local" ]; then
  docker build \
    "${NO_CACHE[@]}" \
    "${BUILD_ARGS[@]}" \
    --build-arg PTY_AGENT_INSTALL_SOURCE=local \
    -t "$IMAGE_NAME" \
    -f "$DOCKERFILE" \
    "$PROJECT_ROOT"

  docker run -it \
    --name "$CONTAINER_NAME" \
    "${RUN_ARGS[@]}" \
    "${ENV_ARGS[@]}" \
    -v "$PROJECT_ROOT":/src/pty-agent \
    -w /workspace \
    "$IMAGE_NAME" \
    bash -lc 'cargo install --path /src/pty-agent --force >/tmp/pty-agent-install.log && exec pty-agent "$@"' \
    bash "$@"
else
  docker build \
    "${NO_CACHE[@]}" \
    "${BUILD_ARGS[@]}" \
    --build-arg PTY_AGENT_INSTALL_SOURCE=crates \
    --build-arg PTY_AGENT_VERSION="$CRATE_VERSION" \
    -t "$IMAGE_NAME" \
    -f "$DOCKERFILE" \
    "$PROJECT_ROOT"

  docker run -it \
    --name "$CONTAINER_NAME" \
    "${RUN_ARGS[@]}" \
    "${ENV_ARGS[@]}" \
    -w /workspace \
    "$IMAGE_NAME" \
    pty-agent "$@"
fi
