# Use an official image for a Python runtime that is based on debian
ARG PYTHON_VERSION=3.12
FROM python:${PYTHON_VERSION}-slim-bookworm

ARG PYTHON_VERSION
ARG ENVIRONMENT_NAME="napari-locan"
# one of pyside2, pyqt5, pyside6, pyqt6
ARG QT_BINDING="pyside6"

ENV PYTHON_VERSION=${PYTHON_VERSION} \
    ENVIRONMENT_NAME=${ENVIRONMENT_NAME} \
    QT_QPA_PLATFORM=offscreen

LABEL maintainer="LocanDevelopers" \
      project="napari-locan" \
      python_version=${PYTHON_VERSION} \
      environment_name=${ENVIRONMENT_NAME}

# set time zone to local time
RUN ln -sf /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime

# Install libraries and remove package manager cache
RUN apt-get update && \
    # Open source implementations of OpenGL and more
    apt-get install -qqy libglib2.0-0 && \
    # following https://github.com/tlambert03/setup-qt-libs
    apt-get install -qqy libegl1 libdbus-1-3 libxkbcommon-x11-0 libxcb-icccm4 \
    libxcb-image0 libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 libxcb-xinerama0 \
    libxcb-xinput0 libxcb-xfixes0 x11-utils libxcb-cursor0 && \
    # install git for setuptools_scm to deal with locan source distribution versioning
    apt-get install -qqy git && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Copy the current directory contents into the container
COPY . /napari-locan

WORKDIR /napari-locan

# Set up and activate virtual environment
ENV VIRTUAL_ENV "/opt/venv"
RUN python -m venv $VIRTUAL_ENV
ENV PATH "$VIRTUAL_ENV/bin:$PATH"

# Update and install any needed packages and the project
RUN pip install pip setuptools wheel pip-tools --trusted-host pypi.org --upgrade --no-cache-dir && \
    # xvfb needed to run tests with pytest_qt fixture\
    pip install $QT_BINDING && \
    pip install pytest-xvfb && \
    pip install .[test]

# Volume for data
VOLUME ["/shared"]

# Run a command when the container launches
CMD today=$(date +"%Y-%m-%d") && \
    base=${ENVIRONMENT_NAME}_$today && \
    pip freeze --all > "/shared/frozen_requirements_$base.txt" && \
    pip-compile --extra test --output-file "/shared/requirements_$base.txt" pyproject.toml && \
    date > "/shared/napari_info_$base.txt" && \
    napari --info >> "/shared/napari_info_$base.txt" && \
    date > "/shared/test_results_$base.txt" && \
    pytest >> "/shared/test_results_$base.txt"
