# Dockerfile

ARG         AWS_ACCOUNT=086679231553
FROM        ${AWS_ACCOUNT}.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

RUN         mkdir -p $PYSETUP_PATH /var/app && \
            chown -R worker:worker $PYSETUP_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==1.8.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        . /var/app


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

EXPOSE      5000
USER        worker
CMD         ["poetry", "run", "ddtrace-run", "python", "dev.py", "2>&1"]


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

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


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

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


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

# Remove cruft
RUN         rm -rf tests/ scripts/ poetry.lock README.md
# Add uvicorn script for fastapi
COPY        uvicorn-start.sh  /

EXPOSE      8080
USER        worker
ENTRYPOINT  ["/uvicorn-start.sh"]
