FROM python:3.12.8-slim-bookworm AS base

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_ROOT_USER_ACTION=ignore \
    APP_HOST=0.0.0.0 \
    APP_PORT=8080 \
    APP_ENV=lab

RUN groupadd --system --gid 10001 app \
 && useradd --system --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app

WORKDIR /srv

COPY requirements.txt ./
RUN pip install -r requirements.txt

COPY app ./app

FROM base AS test
COPY requirements-test.txt ./
RUN pip install -r requirements-test.txt
COPY pytest.ini ./
COPY student-tests ./student-tests
COPY unit-tests ./unit-tests
USER app
CMD ["python", "-m", "pytest", "-v", "--no-header"]

FROM base AS runtime
USER app
EXPOSE 8080
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
  CMD ["python", "-m", "app.healthcheck"]
STOPSIGNAL SIGTERM
CMD ["python", "-m", "app"]
