# Start from the Miniconda base image to use Conda
FROM continuumio/miniconda3:25.3.1-1

# Set the working directory in the container
WORKDIR /auto-importer

# Repair pip's bundled trust store before downloading packages, then use the
# repaired certifi bundle for the first Conda transaction (including libmamba).
# https://discuss.python.org/t/109576
ARG PIP_BOOTSTRAP="pip @ https://files.pythonhosted.org/packages/3f/06/3e4420302363fbb71be3c7f35ce042f54bfca330e3e66b3a2e3c4cca579f/pip-26.2.2-py3-none-any.whl#sha256=fd7f4dfa8190e014013b9f08f3e973f4e4c26ffd179a204895533a7254813a70"
RUN /opt/conda/bin/python -m pip install --no-cache-dir "$PIP_BOOTSTRAP" && \
    /opt/conda/bin/python -m pip install --no-cache-dir --upgrade 'certifi>=2026.10.19' && \
    CONDA_SSL_VERIFY="$(/opt/conda/bin/python -m certifi)" conda install -n base -y \
        'python>=3.13.18,<3.14' 'pip>=26.2.2' 'certifi>=2026.10.19' 'ca-certificates>=2026.10.20'

# Create a Conda environment
RUN conda create -n auto-import-env python=3.12 -y

# Install omero-py, bftools, and psycopg2 using Conda
RUN conda install -n auto-import-env -c conda-forge omero-py -y && \
    conda install -n auto-import-env -c bioconda bftools -y && \
    conda install -n auto-import-env -c conda-forge psycopg2 'libffi>=3.4.8,<3.5' -y && \
    conda install -n auto-import-env -c conda-forge intel-openmp=2019.4 -y

# Enforce security floors after the application dependencies have been solved.
RUN conda install -n auto-import-env -y \
    'python>=3.12.16,<3.13' 'pip>=26.2.2' 'certifi>=2026.10.19' 'ca-certificates>=2026.10.20'

# Activate the environment by setting the path to environment's bin directory
ENV PATH /opt/conda/envs/auto-import-env/bin:$PATH

# Install git and system prerequisites for building PostgreSQL drivers
# And for podman in podman
RUN apt-get update && apt-get install -y \
    ca-certificates \
    git \
    python3-dev \
    libpq-dev \
    build-essential \
    libcap2-bin \
    fuse-overlayfs \
    podman \
    unzip

# Released dependencies are installed from pyproject.toml with the package below.
# For development, uncomment this block and set the branches being tested.
# ARG BIOMERO_SCHEMA_BRANCH=main
# ARG BIOMERO_SHALLOWER_BRANCH=main
# ADD "https://api.github.com/repos/NL-BioImaging/biomero-schema/commits/${BIOMERO_SCHEMA_BRANCH}" /latest_commit_biomero_schema
# ADD "https://api.github.com/repos/NL-BioImaging/BIOMERO.shallower/commits/${BIOMERO_SHALLOWER_BRANCH}" /latest_commit_biomero_shallower
# RUN pip install \
#     "biomero-schema @ git+https://github.com/NL-BioImaging/biomero-schema.git@${BIOMERO_SCHEMA_BRANCH}" \
#     "biomero-shallower @ git+https://github.com/NL-BioImaging/BIOMERO.shallower.git@${BIOMERO_SHALLOWER_BRANCH}"

# Create a group and user with specified GID and UID
RUN groupadd -g 1000 autoimportgroup && \
    useradd -m -r -u 1000 -g autoimportgroup autoimportuser

# ----------------- Setting up Podman in Podman ---------------- #
# Taking inspiration from RHEL/Podman's own blogs:
#   - https://www.redhat.com/en/blog/podman-inside-container
#   - https://github.com/containers/image_build/blob/main/podman/Containerfile
#
# The application and its nested Podman engine run as autoimportuser
# (UID/GID 1000). With VFS storage, the outer rootless container needs:
#
# --userns=keep-id:uid=1000,gid=1000
# --security-opt label=disable
#
# No --privileged, additional capability, or /dev/fuse device is required.
# fuse-overlayfs remains available for deployments that select overlay storage.
# -------------------------------------------------------------- # 

# Pre-create necessary directories in the user's home directory
RUN mkdir -p /home/autoimportuser/.local/share/containers/storage /home/autoimportuser/.config/containers

# Add mappings to /etc/subuid and /etc/subgid. Use printf because Debian's
# /bin/sh writes a literal "-e" when echo -e is used, which invalidates the
# first subordinate-ID range.
RUN printf 'autoimportuser:1:999\nautoimportuser:1001:64535\n' > /etc/subuid && \
    printf 'autoimportuser:1:999\nautoimportuser:1001:64535\n' > /etc/subgid

# Ensure proper permissions for all relevant directories in the user's home directory
RUN chown -R autoimportuser:autoimportgroup /home/autoimportuser/.local /home/autoimportuser/.config /auto-importer

# Add container configuration files
COPY /containers.conf /etc/containers/containers.conf
COPY /storage.conf /etc/containers/storage.conf
COPY /podman-containers.conf /home/autoimportuser/.config/containers/containers.conf

# Set up internal Podman to pass subscriptions down from host to internal container
RUN printf '/run/secrets/etc-pki-entitlement:/run/secrets/etc-pki-entitlement\n/run/secrets/rhsm:/run/secrets/rhsm\n' > /etc/containers/mounts.conf

# Note VOLUME options must always happen after the chown call above
# RUN commands can not modify existing volumes
VOLUME /var/lib/containers
VOLUME /home/autoimportuser/.local/share/containers

# Create necessary directories for shared storage and lock files
RUN mkdir -p /var/lib/shared/overlay-images \
             /var/lib/shared/overlay-layers \
             /var/lib/shared/vfs-images \
             /var/lib/shared/vfs-layers && \
    touch /var/lib/shared/overlay-images/images.lock && \
    touch /var/lib/shared/overlay-layers/layers.lock && \
    touch /var/lib/shared/vfs-images/images.lock && \
    touch /var/lib/shared/vfs-layers/layers.lock

# Nested rootless Podman needs mapping helpers with file capabilities. A setuid
# bit does not provide the required capability through the outer rootless user
# namespace.
RUN chmod 0755 /usr/bin/newuidmap /usr/bin/newgidmap && \
    setcap cap_setuid=ep /usr/bin/newuidmap && \
    setcap cap_setgid=ep /usr/bin/newgidmap

# Set environment variable to allow custom Podman configurations
ENV _CONTAINERS_USERNS_CONFIGURED="" \
    BUILDAH_ISOLATION=chroot \
    PODMAN_BIND_RETRY_ATTEMPTS=3 \
    PODMAN_BIND_RETRY_DELAY_SECONDS=2

# Copy the application code (when building from the repository context)
COPY . /auto-importer

# A submodule build context has a .git file rather than the repository metadata.
# Supply the release tag explicitly so local release builds retain their version.
ARG BIOMERO_IMPORTER_VERSION
RUN if [ -n "$BIOMERO_IMPORTER_VERSION" ]; then \
        SETUPTOOLS_SCM_PRETEND_VERSION_FOR_BIOMERO_IMPORTER="$BIOMERO_IMPORTER_VERSION" pip install '/auto-importer[identity]'; \
    elif [ -d "/auto-importer/.git" ]; then \
        git config --global --add safe.directory /auto-importer && \
        pip install '/auto-importer[identity]'; \
    else \
        SETUPTOOLS_SCM_PRETEND_VERSION_FOR_BIOMERO_IMPORTER=0.0.0 pip install '/auto-importer[identity]'; \
    fi

# Fail the build if subsequent dependency installation regresses either environment.
RUN set -eu; for py in /opt/conda/bin/python /opt/conda/envs/auto-import-env/bin/python; do \
        "$py" -c "import sys; from importlib.metadata import version; from packaging.version import Version; assert sys.version_info[:3] >= {(3, 12): (3, 12, 16), (3, 13): (3, 13, 18)}[sys.version_info[:2]]; assert Version(version('pip')) >= Version('26.2.2'); assert Version(version('certifi')) >= Version('2026.10.19')"; \
    done && python -m pip check

# Make the logs directory
RUN mkdir -p /auto-importer/logs

# Ensure proper permissions for all relevant directories in the user's auto-importer directory
RUN chown -R autoimportuser:autoimportgroup /auto-importer/logs

# Ensure your application's startup script is executable (already in GIT)
RUN chmod +x /auto-importer/biomero_importer/main.py

# Download and install OMERO Java client libraries
RUN mkdir -p /opt/omero/server && \
    cd /tmp && \
    wget -q https://downloads.openmicroscopy.org/omero/5.6.16/artifacts/OMERO.server-5.6.16-ice36.zip && \
    unzip -q OMERO.server-5.6.16-ice36.zip && \
    mv OMERO.server-5.6.16-ice36 /opt/omero/server/OMERO.server && \
    rm OMERO.server-5.6.16-ice36.zip && \
    chown -R autoimportuser:autoimportgroup /opt/omero

# Set OMERODIR to point to the server installation (runtime environment)
ENV OMERODIR /opt/omero/server/OMERO.server

# Switch to the new user for all subsequent commands
USER autoimportuser

# Set the default command or entrypoint to the main script
ENTRYPOINT ["/opt/conda/bin/conda", "run", "-n", "auto-import-env", "python", "-m", "biomero_importer.main"]
