###########
# BUILDER #
###########
FROM ghcr.io/astral-sh/uv:python3.12-bookworm AS python-builder

WORKDIR /app

# Copy the project files needed to configure dependencies build into the image
COPY --chmod=644 pyproject.toml uv.lock README.md ./

RUN uv venv --relocatable
RUN uv sync --frozen --no-install-project --no-editable

#########
# FINAL #
#########
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim

LABEL about.license="MIT License (MIT)"
LABEL about.tags="WGS,WES,Rare diseases,VCF,variants,phenotype,OMIM,HPO,variants,Matchmaker Exchange"
LABEL about.home="https://github.com/Clinical-Genomics/patientMatcher"

RUN groupadd --gid 1000 worker && useradd -g worker --uid 1000 --shell /usr/sbin/nologin --create-home worker

ENV PATH="/home/worker/app/.venv/bin:$PATH"

WORKDIR /home/worker/app

# Copy current app code to app dir
COPY --chown=root:root --chmod=755 . /home/worker/app

# Copy virtual environment from builder
COPY --chown=root:root --chmod=755 --from=python-builder /app/.venv /home/worker/app/.venv

RUN uv pip install --no-cache-dir --editable . gunicorn

USER worker

ENV PYTHONUNBUFFERED=1
ENV LANG=C.UTF-8
ENV GUNICORN_WORKERS=1
ENV GUNICORN_THREADS=1
ENV GUNICORN_BIND="0.0.0.0:8000"
ENV GUNICORN_TIMEOUT=400
ENV GUNICORN_MAXREQUESTS=1200

CMD ["sh", "-c", "/home/worker/app/.venv/bin/gunicorn \
    --workers=$GUNICORN_WORKERS \
    --bind=$GUNICORN_BIND \
    --threads=$GUNICORN_THREADS \
    --timeout=$GUNICORN_TIMEOUT \
    --max-requests=$GUNICORN_MAXREQUESTS \
    --proxy-protocol \
    --forwarded-allow-ips=10.0.2.100,127.0.0.1 \
    --log-syslog \
    --access-logfile=- \
    --error-logfile=- \
    --log-level=debug \
    patientMatcher.server.auto:app"]





