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

ENV         VIRTUAL_ENV=/var/venv
ENV         PATH="$VIRTUAL_ENV/bin:$PATH"

USER        root

RUN         apt-get update && \
            apt-get -y upgrade

RUN         python -m venv ${VIRTUAL_ENV} && \
            pip install --no-cache-dir --upgrade pip setuptools && \
            pip install --no-cache poetry

USER        worker

WORKDIR     /var/app
COPY        pyproject.toml poetry.lock /var/app/

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

USER        root

RUN         apt-get -y install gcc && \
            poetry install --only main --no-root --no-cache && \
            apt-get purge -y gcc && \
            apt-get -y autoremove

USER        worker

COPY        store /var/app/store
COPY        spec /var/app/spec
COPY        application.py uwsgi-start.sh uwsgi.ini /var/app/

EXPOSE      8080
ENTRYPOINT  ["./uwsgi-start.sh"]

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

USER        root
RUN         apt-get -y install gcc && \
            poetry install --extras dev --no-root --no-cache && \
            apt-get purge -y gcc && \
            apt-get -y autoremove

USER        worker

COPY        store /var/app/store
COPY        scripts /var/app/scripts
COPY        application.py dev.py docker-compose.yml /var/app/

EXPOSE      5001
ENTRYPOINT  ["python", "dev.py"]

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

USER        worker

COPY        tests /var/app/tests

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

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

USER        worker

COPY        tests /var/app/tests

ENTRYPOINT  ["scripts/integration.sh"]

#####################################
### Build image for code format   ###
#####################################

FROM        dev AS format

USER        worker

COPY        tests /var/app/tests

ENTRYPOINT  ["scripts/format.sh"]

################################
### Generate poetry lockfile ###
################################
FROM        base AS update-lockfile

# Use root so the poetry.lock file can be rewritten inside of the container
USER        root

COPY        scripts/update-lockfile.sh /var/app/scripts/
ENTRYPOINT  ["scripts/update-lockfile.sh"]
