# atlas-chimera v0.3.0 production image
#
# Bundles:
#   - Atlas v9 encoder checkpoint
#   - KESTREL v8.4 encoder (runs as localhost:8001 HTTP service inside container)
#   - BPE vocabulary
#   - atlas-chimera Python package with vendored atlas_core
#   - Pre-trained ensemble classifier (classifier_v04.joblib) + v0.3 fallback (classifier_v1.joblib)
#
# Two processes inside container, managed by tini + entrypoint.sh:
#   1. KESTREL FastAPI server (background, localhost-only)
#   2. atlas-chimera CLI (foreground, the actual command being run)
#
# GPU version: ghcr.io/sentry-bio/atlas-chimera:0.3.0
# CPU version: ghcr.io/sentry-bio/atlas-chimera:0.3.0-cpu (same Dockerfile, no CUDA base)

ARG BASE_IMAGE=pytorch/pytorch:2.5.1-cuda12.1-cudnn9-runtime
FROM ${BASE_IMAGE}

LABEL org.opencontainers.image.source="https://github.com/sentry-bio/atlas-chimera"
LABEL org.opencontainers.image.description="Reference-free chimera detection for metagenomic contigs and long-read amplicons"
LABEL org.opencontainers.image.licenses="MIT"

# Minimal system deps
RUN apt-get update && apt-get install -y --no-install-recommends \
    tini curl ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Install the Python package
WORKDIR /opt
COPY atlas_chimera /opt/atlas-chimera/atlas_chimera
COPY pyproject.toml /opt/atlas-chimera/pyproject.toml
COPY README.md /opt/atlas-chimera/README.md
COPY LICENSE /opt/atlas-chimera/LICENSE
RUN pip install --no-cache-dir /opt/atlas-chimera

# Install KESTREL server dependencies (FastAPI, uvicorn)
RUN pip install --no-cache-dir fastapi uvicorn[standard] httpx

# Bundle model weights (must be present in build context at these paths)
# Build this image with `docker build -f docker/Dockerfile -t atlas-chimera:0.3.0 .` from the repo root,
# after running `scripts/fetch_models.sh` to download weights into models/
RUN mkdir -p /models
COPY models/atlas_v9/best.pt /models/atlas_v9/best.pt
COPY models/kestrel_v8_4/best.pt /models/kestrel_v8_4/best.pt
COPY models/bpe_vocab.json /models/bpe_vocab.json

# Bundle KESTREL server code (copied from v8_4_deploy)
COPY docker/kestrel_server /opt/kestrel_server
ENV KESTREL_CHECKPOINT=/models/kestrel_v8_4/best.pt
ENV KESTREL_BPE_VOCAB=/models/bpe_vocab.json

ENV CHECKPOINT_PATH=/models/atlas_v9/best.pt
ENV BPE_VOCAB_PATH=/models/bpe_vocab.json
ENV KESTREL_URL=http://127.0.0.1:8001

# Entrypoint: start KESTREL in background, then exec atlas-chimera
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

# tini for clean signal handling with two processes
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/entrypoint.sh"]
CMD ["atlas-chimera", "--help"]
