# This Dockerfile is used both to specify a functional development environment
# (e.g. on GitHub Codespaces) and a deployment image.

# Must match .python-version, which constraints.txt is compiled for.
# `psynet scripts update` sets both to the active Python version.
FROM python:{python_major_minor}-trixie

RUN pip install uv

# Node and npm are for development tooling.
RUN apt-get update && apt-get install -y nodejs npm

RUN mkdir /experiment
WORKDIR /experiment

COPY requirements.txt requirements.txt
COPY *constraints.txt constraints.txt

# ``psynet debug ssh --use-local-psynet`` and ``--use-local-dallinger`` stage
# local-image-pins.txt: the pins without the packages whose local wheels are
# installed further down.
COPY *local-image-pins.txt ./

ENV SKIP_DEPENDENCY_CHECK=""
ENV DALLINGER_NO_EGG_BUILD=1

RUN pins=requirements.txt; \
    if [ -f constraints.txt ]; then pins=constraints.txt; fi; \
    if [ -f local-image-pins.txt ]; then pins=local-image-pins.txt; fi; \
    uv pip install -r "$pins" --system

COPY *prepare_docker_image.sh prepare_docker_image.sh
RUN if test -f prepare_docker_image.sh ; then bash prepare_docker_image.sh ; fi

COPY . /experiment

# ``psynet debug ssh --use-local-psynet`` stages a wheel of the local PsyNet
# checkout, which replaces the requirements.txt version.
# ``uv pip check`` fails the build if a local wheel conflicts with the other
# installed packages.
RUN set -- /experiment/psynet-*.whl; \
    if [ -f "$1" ]; then \
        uv pip install --system --reinstall-package psynet "psynet[experiment] @ file://$1" \
        && uv pip check --system; \
    fi

# A local Dallinger wheel staged by ``DALLINGER_SOURCE`` / editable install
# must win over the Git pin installed from requirements/constraints above.
# Its dependencies are resolved too, keeping the pinned versions that satisfy them.
RUN set -- /experiment/dallinger-*.whl; \
    if [ -f "$1" ]; then \
        uv pip install --system --reinstall-package dallinger "dallinger[docker] @ file://$1" \
        && uv pip check --system; \
    fi

# The SSH account is chosen when the container starts. /experiment must be
# writable so that account can create server.log.
# Shipped source files stay owned by root.
RUN chmod a+rwx /experiment

ENV PORT=5000

CMD ["dallinger_heroku_web"]
