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

ENV         PYTHONUNBUFFERED=1 \
            PYTHONDONTWRITEBYTECODE=1 \
            POETRY_HOME="/opt/poetry" \
            POETRY_VIRTUALENVS_IN_PROJECT=1 \
            POETRY_VIRTUALENVS_CREATE=1 \
            VIRTUAL_ENV="/opt/pysetup/.venv"
ENV         PATH="$POETRY_HOME/bin:$VIRTUAL_ENV/bin:$PATH"

WORKDIR     $VIRTUAL_ENV

USER        root
# Update and install common packages
RUN         apt-get -y update && \
            apt-get -y upgrade && \
            python -m venv $VIRTUAL_ENV && \
            $VIRTUAL_ENV/bin/pip install --no-cache --upgrade pip setuptools && \
            $VIRTUAL_ENV/bin/pip install --no-cache poetry==2.3.4

USER        worker

# Switch to the app directory
WORKDIR     /var/app
COPY        pyproject.toml poetry.lock /var/app/

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        spec /var/app/spec
COPY        video /var/app/video
COPY        application.py uwsgi.ini /var/app/
COPY        uwsgi-start.sh /

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

FROM        base AS dev-base

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        application.py dev.py docker-compose.yml docker-compose.unit-lint.yml /var/app/
COPY        video /var/app/video
COPY        spec /var/app/spec

FROM        dev-base AS unit-lint

COPY        tests /var/app/tests
COPY        scripts/unit-lint.sh /var/app/scripts/

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

FROM        dev-base AS dev

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

FROM        dev-base AS integration

COPY        tests/__init__.py /var/app/tests/
COPY        tests/integration /var/app/tests/integration
COPY        scripts/integration.sh /var/app/scripts/

ENTRYPOINT [ "scripts/integration.sh" ]

FROM        dev-base AS format

COPY        tests /var/app/tests
COPY        scripts/format.sh /var/app/scripts/

ENTRYPOINT  ["scripts/format.sh"]

FROM        base AS update-lockfile

COPY        scripts/update-lockfile.sh /var/app/scripts/

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

ENTRYPOINT  ["scripts/update-lockfile.sh"]
