# Dexmond Agent 3 — ATLAS API server
FROM python:3.12-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    ATLAS_ENV=production \
    ATLAS_HOST=0.0.0.0 \
    ATLAS_PORT=8768 \
    ATLAS_DB_PATH=/data/atlas_agent.db \
    ATLAS_DISCOVER_ON_START=false

RUN groupadd --system atlas && useradd --system --gid atlas --home /app atlas \
    && mkdir -p /data && chown atlas:atlas /data

WORKDIR /app
ARG ATLAS_WITH_OCR=1
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends tzdata \
    && if [ "$ATLAS_WITH_OCR" = "1" ]; then apt-get install -y --no-install-recommends tesseract-ocr tesseract-ocr-eng poppler-utils; fi \
    && rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY --chown=atlas:atlas atlas_agent ./atlas_agent

LABEL org.opencontainers.image.title="Dexmond ATLAS" \
      org.opencontainers.image.vendor="Dexmond Technologies Ltd, Ireland" \
      org.opencontainers.image.version="1.2.1"

USER atlas
VOLUME ["/data"]
EXPOSE 8768

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD python -c "import urllib.request,sys; urllib.request.urlopen('http://127.0.0.1:8768/healthz', timeout=4)" || exit 1

CMD ["python", "-m", "atlas_agent.server"]
