# Build from the repository/deployment-bundle root:
# docker build -f codex_pcb/Dockerfile -t vibemake-pcb-backend:local .
ARG BASE_IMAGE=node:22-bookworm-slim
FROM ${BASE_IMAGE}

ARG DEBIAN_MIRROR=http://mirrors.tuna.tsinghua.edu.cn/debian
ARG DEBIAN_SECURITY_MIRROR=http://mirrors.tuna.tsinghua.edu.cn/debian-security

ENV DEBIAN_FRONTEND=noninteractive \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PATH=/opt/venv/bin:$PATH

RUN set -eu; \
    for source in /etc/apt/sources.list /etc/apt/sources.list.d/*.sources /etc/apt/sources.list.d/*.list; do \
      [ -f "$source" ] || continue; \
      sed -i \
        -e "s|https://deb[.]debian[.]org/debian-security|${DEBIAN_SECURITY_MIRROR%/}|g" \
        -e "s|http://deb[.]debian[.]org/debian-security|${DEBIAN_SECURITY_MIRROR%/}|g" \
        -e "s|https://security[.]debian[.]org/debian-security|${DEBIAN_SECURITY_MIRROR%/}|g" \
        -e "s|http://security[.]debian[.]org/debian-security|${DEBIAN_SECURITY_MIRROR%/}|g" \
        -e "s|https://deb[.]debian[.]org/debian|${DEBIAN_MIRROR%/}|g" \
        -e "s|http://deb[.]debian[.]org/debian|${DEBIAN_MIRROR%/}|g" \
        "$source"; \
    done

# BuildKit caches survive a failed build; no package cache is added to the image.
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
    rm -f /etc/apt/apt.conf.d/docker-clean \
    && apt-get -o Acquire::Retries=3 update \
    && apt-get install -y --no-install-recommends \
       python3 python3-venv ca-certificates curl tini \
       openjdk-17-jre blender xvfb xauth x11-utils dbus-x11 \
       libasound2 libatk-bridge2.0-0 libgtk-3-0 libnss3 libxss1 \
       libgbm1 libdrm2 libx11-xcb1 libxtst6 libsecret-1-0 libfuse2 \
       fonts-noto-cjk \
    && python3 -m venv /opt/venv

ARG CODEX_VERSION=0.169.0-alpha.11.1
ARG NPM_REGISTRY=https://registry.npmmirror.com
RUN --mount=type=cache,target=/root/.npm \
    npm install -g --registry="${NPM_REGISTRY}" "@openai/codex@${CODEX_VERSION}"

COPY codex_pcb/requirements.txt /tmp/pcb-requirements.txt
COPY codex_pcb/requirements-delivery.txt /tmp/requirements-delivery.txt
ARG PIP_INDEX_URL=https://mirrors.ustc.edu.cn/pypi/simple
RUN --mount=type=cache,target=/root/.cache/pip \
    python -m pip install --index-url "${PIP_INDEX_URL}" -r /tmp/pcb-requirements.txt

COPY backend/easyeda_backend/skills/easyeda-api/references/ /opt/pcb-resources/references/
COPY backend/easyeda_backend/skills/easyeda-api/SKILL.md /usr/share/doc/vibemake-pcb/easyeda-api-provenance.md
COPY LICENSE codex_pcb/THIRD-PARTY.md /usr/share/doc/vibemake-pcb/
COPY codex_pcb/build-assets/ /tmp/pcb-build-assets/
ARG FREEROUTING_SHA256=9084a4888937a7f31f857ecc12aa7a37407f51160e4d2892dff9c9bb47ae3102
ARG FREEROUTING_URL=https://github.com/freerouting/freerouting/releases/download/v1.9.0/freerouting-1.9.0.jar
ARG FREEROUTING_LICENSE_URL=https://raw.githubusercontent.com/freerouting/freerouting/v1.9.0/LICENSE
RUN if [ -s /tmp/pcb-build-assets/freerouting.jar ]; then \
      cp /tmp/pcb-build-assets/freerouting.jar /opt/pcb-resources/freerouting.jar; \
    else \
      curl --fail --location --retry 3 "${FREEROUTING_URL}" \
        --output /opt/pcb-resources/freerouting.jar; \
    fi \
    && echo "${FREEROUTING_SHA256}  /opt/pcb-resources/freerouting.jar" | sha256sum --check - \
    && if [ -s /tmp/pcb-build-assets/LICENSE.freerouting ]; then \
      cp /tmp/pcb-build-assets/LICENSE.freerouting /usr/share/doc/vibemake-pcb/LICENSE.freerouting; \
    else \
      curl --fail --location --retry 3 "${FREEROUTING_LICENSE_URL}" \
        --output /usr/share/doc/vibemake-pcb/LICENSE.freerouting; \
    fi \
    && useradd --uid 10001 --create-home --shell /bin/bash pcb \
    && mkdir -p /data/home /data/codex-home /data/tasks \
    && chown -R pcb:pcb /data

WORKDIR /app
COPY codex_pcb/*.py codex_pcb/instructions.md /app/codex_pcb/
COPY --chmod=755 codex_pcb/docker-entrypoint.sh /usr/local/bin/pcb-entrypoint

ENV HOME=/data/home \
    PCB_SESSION_ROOT=/data \
    PCB_CODEX_HOME=/data/codex-home \
    PCB_CODEX_AUTH=apikey \
    PCB_CODEX_BIN=/usr/local/bin/codex \
    PCB_EDA_BIN=/opt/lceda-pro/lceda-pro \
    PCB_BLENDER_BIN=/usr/bin/blender \
    PCB_FREEROUTING_JAR=/opt/pcb-resources/freerouting.jar \
    DISPLAY=:99 \
    XDG_RUNTIME_DIR=/tmp/pcb-xdg \
    LIBGL_ALWAYS_SOFTWARE=1

USER pcb
EXPOSE 8003
HEALTHCHECK --interval=30s --timeout=5s --start-period=210s --retries=3 \
    CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8003/api/ready', timeout=4).read()"]
ENTRYPOINT ["/usr/bin/tini", "-g", "--", "/usr/local/bin/pcb-entrypoint"]
CMD ["python", "-m", "uvicorn", "codex_pcb.app:app", "--host", "0.0.0.0", "--port", "8003", "--workers", "1"]
