# Dockerfile

ARG         AWS_ACCOUNT=086679231553
FROM        ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python313 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 && \
                mkdir -p $PYSETUP_PATH /var/app && \
                chown -R root:root $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

# Create the volume `/mnt` and give worker user the permissions
ENV         MOUNT_PATH="/mnt/pdp-backfill"
RUN         mkdir -p $MOUNT_PATH && chown -R worker:worker $MOUNT_PATH
VOLUME      $MOUNT_PATH

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

# 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        ./backfill ./backfill
COPY        ./pdp_qa_refresh ./pdp_qa_refresh
COPY        pyproject.toml poetry.lock ./
RUN         touch README.md


###########################
### 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
COPY        ./scripts ./scripts

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

RUN         --mount=type=cache,target=$POETRY_CACHE_DIR \
            poetry install --only main
USER        worker
ENTRYPOINT  [ "scripts/integration.sh" ]



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

RUN         poetry install --only main

# Remove cruft
RUN         rm -rf tests/ scripts/ poetry.lock README.md

USER        worker
ENTRYPOINT  [ "poetry", "run", "pdpbackfill" ]
CMD         [ "greetings", "hello", "pp" ]
