# syntax=docker/dockerfile:1
ARG PYTHON_IMAGE=python:3.13-slim-bookworm
FROM ${PYTHON_IMAGE} AS builder
ENV PIP_DISABLE_PIP_VERSION_CHECK=1
WORKDIR /build
COPY pyproject.toml requirements.lock ./
COPY accessscope ./accessscope
RUN python -m pip wheel --no-cache-dir --constraint requirements.lock --wheel-dir /wheels '.[ldap,identity,postgres]'

FROM ${PYTHON_IMAGE} AS runtime
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    HOME=/var/lib/accessscope \
    ACCESSSCOPE_DATA_DIR=/var/lib/accessscope \
    ACCESSSCOPE_BACKUP_DIR=/backups
RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends ca-certificates smbclient krb5-user snmp \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd --gid 10001 accessscope \
    && useradd --uid 10001 --gid accessscope --no-create-home --home-dir /var/lib/accessscope --shell /usr/sbin/nologin accessscope \
    && mkdir -p /var/lib/accessscope /backups \
    && chown 10001:10001 /var/lib/accessscope /backups \
    && chmod 0700 /var/lib/accessscope /backups
COPY --from=builder /wheels /wheels
RUN python -m pip install --no-cache-dir --no-index --find-links /wheels 'accessscope-enterprise[ldap,identity,postgres]==0.9.0' \
    && python -m pip check \
    && rm -rf /wheels
WORKDIR /app
USER 10001:10001
EXPOSE 8443 9443 9080
STOPSIGNAL SIGTERM
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
    CMD ["python", "-m", "accessscope.container", "healthcheck"]
ENTRYPOINT ["python", "-m", "accessscope.container"]
CMD ["serve"]
