ARG         AWS_ACCOUNT=086679231553
FROM        ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python311 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==2.3.4

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 update && \
            apt-get -y install gcc && \
            poetry install --only main --no-root --no-cache && \
            apt-get purge -y gcc && \
            apt-get -y autoremove

USER        worker

WORKDIR     /var/app
COPY        assets /var/app/assets
COPY        application.py uwsgi.ini uwsgi-start.sh /var/app/

EXPOSE 8080

ENTRYPOINT ["/var/app/uwsgi-start.sh"]

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

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

USER        worker

WORKDIR     /var/app
COPY        assets /var/app/assets
COPY        scripts /var/app/scripts
COPY        application.py dev.py docker-compose.yml docker-compose.unit-lint.yml /var/app/


EXPOSE      5000
ENTRYPOINT  ["python", "dev.py", "2>&1"]

#####################################
### Build image for code format   ###
#####################################
FROM       dev AS format

USER       worker
COPY       tests /var/app/tests

ENTRYPOINT ["scripts/format.sh"]

#####################################
### Build image for unit-lint     ###
#####################################
FROM        dev AS unit-lint
ENV         RUFF_CACHE_DIR=/tmp/.ruff_cache

USER        worker
COPY        tests /var/app/tests

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

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

USER        worker
COPY        tests /var/app/tests

ENTRYPOINT ["scripts/integration.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"]
