# Dockerfile

ARG         AWS_ACCOUNT=086679231553
FROM        ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python311 AS base

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    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="$VIRTUAL_ENV/bin:$PATH"


USER        root

# Update common packages
RUN         apt-get update && apt-get upgrade -y && \
                apt-get -y clean && \
                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
                pip install --upgrade setuptools

# Install poetry
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==2.3.3


# 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        ./m2mconfig ./m2mconfig
COPY        m2m.json pyproject.toml poetry.lock ./


###########################
### 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

RUN         touch README.md && \
                poetry install --only main

COPY        ./scripts ./scripts
COPY        ./tests ./tests


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

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


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

USER        worker
ENTRYPOINT  [ "scripts/integration.sh" ]



##############################
### Build image for deploy ###
##############################
FROM        base AS deploy

WORKDIR     $PYSETUP_PATH
RUN         rm pyproject.toml poetry.lock

WORKDIR     /var/app
RUN         touch README.md && \
                poetry install --only main && \
                rm README.md pyproject.toml poetry.lock && \
                pip uninstall -y poetry setuptools wheel && \
                pip uninstall -y pip

USER        worker
ENTRYPOINT  [ "m2mconfig" ]
CMD         [ "greetings", "bonjour", "pp-team" ]
