# Dockerfile

FROM        086679231553.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python312 AS base

ENV         PYTHONUNBUFFERED=1 \
            PYTHONDONTWRITEBYTECODE=1 \
            POETRY_HOME="/opt/poetry" \
            PYSETUP_PATH="/opt/pysetup" \
            POETRY_VIRTUALENVS_IN_PROJECT=1 \
            POETRY_VIRTUALENVS_CREATE=1 \
            POETRY_CACHE_DIR="/tmp/poetry_cache" \
            VIRTUAL_ENV="/opt/pysetup/.venv"

ENV         PATH="$POETRY_HOME/bin:$VIRTUAL_ENV/bin:$PATH"

USER        root

# Update and install common packages
RUN         apt-get update && apt-get upgrade -y \
            && apt-get -y clean

RUN         mkdir -p $PYSETUP_PATH /var/app && \
            chown -R worker:worker $PYSETUP_PATH

# Upgrade setuptools from the Docker Parent Image, even if
# it is not the one we rely on for our Python virtual environment
RUN         pip install --upgrade setuptools

# Install poetry into dedicated virtual environment
RUN         --mount=type=cache,target=/root/.cache/pip \
            python -m venv $VIRTUAL_ENV && \
            $VIRTUAL_ENV/bin/pip install --upgrade pip setuptools && \
            $VIRTUAL_ENV/bin/pip install poetry


# Switch to the poetry setup dir and install requirements
WORKDIR     $PYSETUP_PATH
COPY        pyproject.toml poetry.lock ./
RUN         --mount=type=cache,target=$POETRY_CACHE_DIR \
            poetry install --only main --no-root

# Switch to the app directory
WORKDIR     /var/app
COPY        pyproject.toml ./
COPY        accounting_run_utils ./accounting_run_utils
COPY        --chmod=755 script_wrapper.sh ./


###########################
### Build image for dev ###
###########################
FROM        base AS dev

# httpretty install complains about some unicode error without this
ENV         LC_ALL=en_US.utf-8
ENV         LC_CTYPE=en_US.UTF-8

# Install dev/test requirements to PYSETUP_PATH
WORKDIR     $PYSETUP_PATH
RUN         --mount=type=cache,target=$POETRY_CACHE_DIR \ 
            poetry install --only=dev --no-root

# change to the app dir.
WORKDIR     /var/app
COPY        tests ./tests


#################################
### Build image for unit-lint ###
#################################
FROM        dev AS unit-lint

USER        worker
COPY        scripts ./scripts

ENTRYPOINT  ["scripts/unit-lint.sh"]


########################################
### Build image for integration      ###
########################################
FROM        dev AS integration

USER        worker
COPY        scripts ./scripts

ENTRYPOINT  ["scripts/integration.sh"]
