# Dockerfile

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

WORKDIR /var/app

USER root
# Update and install common packages
RUN dnf install -y tar zip libffi-devel glibc-langpack-en && \
    dnf clean all && \
    rm -rf /var/cache/dnf/*

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

RUN dnf install -y gcc && \
    pip install --upgrade pip setuptools && \
    pip install -r requirements.txt && \
    pip install uwsgi && \
    dnf clean all && \
    rm -rf /var/cache/dnf/*

# Add application and install requirements
COPY application.py ./
COPY ows_accounting ./ows_accounting

# Explicitly set locale and related env vars to use not default POSIX, but en_US.UTF-8
ENV LC_ALL=en_US.UTF-8
ENV LANG=en_US.UTF-8

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

# Add startup script
COPY uwsgi-start.sh /

USER worker
ENTRYPOINT ["/uwsgi-start.sh"]

EXPOSE 8080

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

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

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

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

# Install dev/test requirements
COPY requirements-dev.txt ./
RUN pip install -r requirements-dev.txt
COPY tests ./tests
COPY .flake8 ./
COPY scripts/unit-lint.sh ./scripts/

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

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

# Install dev/test requirements
COPY requirements-dev.txt ./
RUN pip install -r requirements-dev.txt
COPY tests ./tests
COPY scripts/test-integration.sh ./scripts/

ENTRYPOINT ["scripts/test-integration.sh"]
