# Dockerfile

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

WORKDIR /var/app

USER root
RUN apt-get -y update && apt-get -y install jq && apt-get -y clean

USER worker

# Add and install requirements
COPY requirements.txt .
RUN pip3 install --no-cache-dir -r requirements.txt
ENV PATH="${PATH}:/home/worker/.local/bin"

# Normal deployed app
FROM base AS deploy

# Add application
COPY entrypoint.sh handler.py .
COPY daemon_asset_copy ./daemon_asset_copy

CMD ["/bin/bash", "-c", "source entrypoint.sh"]

# Dev app with fixture infrastructure
FROM deploy AS dev

COPY local_dev_kit/ .

CMD ["/bin/bash", "-c", "source dev_entrypoint.sh"]

# Run tests
FROM base AS test

COPY requirements-dev.txt .
RUN pip3 install -r requirements-dev.txt
COPY tests ./tests

# Add application
COPY entrypoint.sh handler.py .
COPY daemon_asset_copy ./daemon_asset_copy

CMD ["py.test", "tests", "--cov", "daemon_asset_copy", "handler.py", "--cov-report", "xml", "--junitxml=pyunit.xml"]

# Run linting and formatting
FROM base AS lint

COPY requirements-dev.txt .
RUN pip3 install --no-cache-dir -r requirements-dev.txt

# https://click.palletsprojects.com/en/8.1.x/unicode-support/
USER root
RUN apt-get -y update && apt-get -y install locales git && apt-get -y clean
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    dpkg-reconfigure --frontend=noninteractive locales && \
    update-locale LANG=en_US.UTF-8
USER worker
ENV LC_ALL=en_US.UTF-8
ENV LANG=en_US.UTF-8

# install pre-commit
COPY pre-commit-config.yaml .flake8 .isort.cfg .
RUN mv pre-commit-config.yaml .pre-commit-config.yaml
RUN git init
RUN pre-commit install

# copy after pre-commit install for caching
COPY --chown=worker .git ./.git

# clear any global hooks that may exists, exit code 5 if none exist
RUN git config --unset-all core.hooksPath || true

# Add files to check
COPY entrypoint.sh handler.py .
COPY tests ./tests
COPY daemon_asset_copy ./daemon_asset_copy

CMD ["pre-commit", "run", "--all-files"]
