# Dockerfile

ARG         AWS_ACCOUNT=086679231553
FROM        ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python313 AS base

USER        root

# Update and install common packages
RUN         apt-get update && apt-get -y upgrade
RUN         apt-get -y install curl tar zip libffi-dev

USER        worker

WORKDIR     /var/app

# Create and configure virtualenv and uwsgi
ENV         VIRTUAL_ENV=/var/app
RUN         python3.13 -m venv ${VIRTUAL_ENV}
ENV         PATH="$VIRTUAL_ENV/bin:$PATH"

RUN         python -m pip install --upgrade pip setuptools wheel

COPY        requirements.txt ./
COPY        notifications /var/app/notifications
COPY        application.py requirements.txt uwsgi.ini /var/app/
RUN         pip install -I -r /var/app/requirements.txt

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

# Setup UWSGI

USER        root
RUN         useradd uwsgi -s /bin/false
RUN         apt-get -y install gcc \
  && pip install uwsgi==2.0.30 \
  && apt-get -y remove gcc \
  && apt-get -y autoremove \
  && rm -rf /var/lib/apt/lists/*

# Add startup script
COPY         --chmod=755 uwsgi-start.sh /

ENTRYPOINT  ["/uwsgi-start.sh"]

EXPOSE      8080


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

COPY        dev.py .flake8 requirements-dev.txt ./
RUN         pip install -r /var/app/requirements-dev.txt

# Enables prints in python to flush to stdout
ENV         PYTHONUNBUFFERED=1
# Start dev server
ENTRYPOINT  ["/var/app/bin/python"]
CMD         ["dev.py", "2>&1"]


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

COPY        tests /var/app/tests
COPY        --chmod=755 scripts/unit-lint.sh /var/app/scripts/

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


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

COPY        tests /var/app/tests
COPY        --chmod=755 scripts/integration.sh /var/app/scripts/

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