ARG CODE_QUEUE_BASE_IMAGE=oven/bun:1-debian
FROM ${CODE_QUEUE_BASE_IMAGE}

ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
ENV UNIDESK_SKILLS_PATH=/root/.agents/skills
ENV RUSTUP_HOME=/usr/local/rustup
ENV CARGO_HOME=/usr/local/cargo
ENV PATH=/usr/local/cargo/bin:${PATH}

COPY scripts/tran /usr/local/bin/tran
COPY scripts/hwpod /usr/local/bin/hwpod
RUN chmod 755 /usr/local/bin/tran /usr/local/bin/hwpod

RUN (command -v docker >/dev/null 2>&1 && docker buildx version >/dev/null 2>&1 && command -v gh >/dev/null 2>&1 && command -v rg >/dev/null 2>&1 && command -v cargo >/dev/null 2>&1 && command -v rustc >/dev/null 2>&1 && command -v rustfmt >/dev/null 2>&1 && command -v xvfb-run >/dev/null 2>&1) \
  || (apt-get update \
  && apt-get install -y --no-install-recommends \
    bash \
    bubblewrap \
    ca-certificates \
    curl \
    docker-buildx \
    docker-cli \
    docker-compose \
    file \
    g++ \
    gcc \
    gh \
    git \
    gzip \
    iproute2 \
    iptables \
    iputils-ping \
    jq \
    make \
    npm \
    openssh-client \
    patch \
    pkg-config \
    procps \
    python3 \
    python3-pip \
    ripgrep \
    cargo \
    rustc \
    rustfmt \
    rsync \
    tar \
    tini \
    unzip \
    xauth \
    xvfb \
    xz-utils \
  && mkdir -p /usr/local/lib/docker/cli-plugins /root/.docker/cli-plugins \
  && ln -sf /usr/bin/docker-compose /usr/local/lib/docker/cli-plugins/docker-compose \
  && ln -sf /usr/bin/docker-compose /root/.docker/cli-plugins/docker-compose \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*)

RUN (command -v codex >/dev/null 2>&1 && command -v opencode >/dev/null 2>&1 && command -v playwright >/dev/null 2>&1) \
  || (npm config set fetch-retries 5 \
  && npm config set fetch-retry-factor 2 \
  && npm config set fetch-retry-mintimeout 20000 \
  && npm config set fetch-retry-maxtimeout 120000 \
  && for attempt in 1 2 3 4 5; do npm install -g @openai/codex@0.128.0 opencode-ai@1.14.48 playwright@1.59.1 && break; if [ "$attempt" = "5" ]; then exit 1; fi; echo "npm_global_retry=$attempt" >&2; sleep $((attempt * 20)); done)

RUN test -x "$PLAYWRIGHT_BROWSERS_PATH/chromium_headless_shell-1217/chrome-headless-shell-linux64/chrome-headless-shell" \
  || for attempt in 1 2 3; do playwright install --with-deps chromium && break; if [ "$attempt" = "3" ]; then exit 1; fi; echo "playwright_install_retry=$attempt" >&2; sleep $((attempt * 30)); done

RUN command -v xauth >/dev/null 2>&1 \
  || (apt-get update \
  && apt-get install -y --no-install-recommends xauth \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*)

RUN (command -v rustc >/dev/null 2>&1 && rustc --version | awk '{ split($2, v, "."); exit ! (v[1] > 1 || (v[1] == 1 && v[2] >= 86)) }' && command -v rustfmt >/dev/null 2>&1) \
  || (apt-get update \
  && apt-get install -y --no-install-recommends ca-certificates curl rustup \
  && env -u HTTP_PROXY -u HTTPS_PROXY -u http_proxy -u https_proxy bash -lc 'set -e; for attempt in 1 2 3; do rustup toolchain install stable --profile minimal && break; if [ "$attempt" = "3" ]; then exit 1; fi; echo "rustup_toolchain_retry=$attempt" >&2; sleep $((attempt * 15)); done; . "$CARGO_HOME/env" 2>/dev/null || true; hash -r 2>/dev/null || true; rustup default stable; for attempt in 1 2 3; do rustup component add rustfmt && break; if [ "$attempt" = "3" ]; then exit 1; fi; echo "rustup_rustfmt_retry=$attempt" >&2; sleep $((attempt * 15)); done' \
  && rustc --version | awk '{ split($2, v, "."); exit ! (v[1] > 1 || (v[1] == 1 && v[2] >= 86)) }' \
  && rustfmt --version >/dev/null \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*)

WORKDIR /app/src/components/microservices/code-queue
COPY src/components/microservices/code-queue/package.json ./package.json
RUN test -d node_modules/typescript || bun install
WORKDIR /app
COPY package.json /app/package.json
COPY bun.lock /app/bun.lock
RUN bun install
COPY src/components/shared /app/src/components/shared
COPY src/components/frontend/package.json /app/src/components/frontend/package.json
WORKDIR /app/src/components/frontend
RUN test -d node_modules/react || bun install
WORKDIR /app/src/components/microservices/code-queue
COPY src/components/microservices/code-queue/tsconfig.json ./tsconfig.json
COPY src/components/microservices/code-queue/src ./src
RUN mkdir -p /root/.agents/skills

EXPOSE 4222
ENTRYPOINT ["tini", "--"]
CMD ["bun", "--smol", "run", "src/index.ts"]
