FROM python:3.12-slim-bookworm AS builder
ENV PIP_DISABLE_PIP_VERSION_CHECK=1
WORKDIR /src
COPY . .
RUN python scripts/build_source.py \
    && python -m pip wheel --no-cache-dir --wheel-dir /wheels '.[server]'

FROM python:3.12-slim-bookworm
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 \
    PORT=8000 RESULTS_DIR=/app/runtime/results REQUIRE_SOURCE=true MAX_CONCURRENT_JOBS=1
RUN groupadd --gid 10001 noway \
    && useradd --uid 10001 --gid 10001 --no-create-home noway \
    && mkdir -p /app/runtime/results \
    && chown -R 10001:10001 /app
COPY --from=builder /wheels /wheels
RUN python -m pip install --no-cache-dir /wheels/* \
    && rm -rf /wheels
WORKDIR /app
USER 10001:10001
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
    CMD python -c "import os,urllib.request; urllib.request.urlopen('http://127.0.0.1:'+os.environ.get('PORT','8000')+'/healthz',timeout=3).close()"
ENTRYPOINT ["python", "-m", "noway_service"]
CMD ["web", "--host", "0.0.0.0"]
