# Dockerfile

ARG         AWS_ACCOUNT=086679231553
ARG         UV_VERSION=0.8.18
ARG         PARENT_IMAGE_TAG=lambda-python313
FROM        ghcr.io/astral-sh/uv:${UV_VERSION} AS uv
FROM        ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:${PARENT_IMAGE_TAG} AS base

ENV         PYTHONUNBUFFERED=1 \
            UV_COMPILE_BYTECODE=1 \
            UV_LINK_MODE=copy \
            UV_PYTHON_DOWNLOADS=never \
            UV_CACHE_DIR="/tmp/uv_cache"

USER        root

WORKDIR     /var/task

COPY        --from=uv /uv /uvx /bin/

RUN         dnf -y install gcc && \
            dnf clean all


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

ENV         LC_ALL=en_US.utf-8 \
            LC_CTYPE=en_US.UTF-8 \
            PYTHONPYCACHEPREFIX=/tmp/pycache \
            XDG_CACHE_HOME=${LAMBDA_TASK_ROOT}/.cache \
            PYTEST_ADDOPTS=--basetemp=/tmp/pytest \
            COVERAGE_FILE=/tmp/.coverage

COPY        uv.lock pyproject.toml ./
RUN         --mount=type=cache,target=$UV_CACHE_DIR \
            uv sync --locked --no-install-project && \
            dnf clean all

COPY        snapshot_contracts/ ./snapshot_contracts
COPY        config.py ./
COPY        app.py ./

USER        worker

ENV         UV_NO_SYNC=1

ENTRYPOINT  []
CMD         ["/bin/sh"]


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

USER        root

COPY        ./tests ./tests
COPY        --chmod=755 lint-and-test.sh docker-compose.yaml ./

USER        worker

ENTRYPOINT  ["./lint-and-test.sh"]


##########################################
### Build image for integration tests  ###
##########################################
FROM        base AS integration

ENV         LC_ALL=en_US.utf-8 \
            LC_CTYPE=en_US.UTF-8 \
            PYTHONPYCACHEPREFIX=/tmp/pycache \
            XDG_CACHE_HOME=${LAMBDA_TASK_ROOT}/.cache \
            PYTEST_ADDOPTS=--basetemp=/tmp/pytest \
            COVERAGE_FILE=/tmp/.coverage

COPY        uv.lock pyproject.toml ./
RUN         --mount=type=cache,target=$UV_CACHE_DIR \
            uv sync --locked --no-install-project --group integration && \
            dnf clean all

COPY        snapshot_contracts/ ./snapshot_contracts
COPY        config.py ./
COPY        app.py ./
COPY        ./tests ./tests
COPY        --chmod=755 lint-and-test.sh docker-compose.yaml ./

USER        worker

ENV         UV_NO_SYNC=1

ENTRYPOINT  ["./lint-and-test.sh"]


####################################
### Builder image for deployment ###
####################################
FROM        base AS builder

RUN         dnf -y install gcc && \
            dnf clean all

RUN         --mount=type=cache,target=$UV_CACHE_DIR \
            --mount=type=bind,source=uv.lock,target=uv.lock \
            --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
            uv export --frozen --no-emit-workspace --no-dev --no-editable -o requirements.txt && \
            uv pip install -r requirements.txt --target "${LAMBDA_TASK_ROOT}" && \
            dnf clean all

COPY        snapshot_contracts/ ./snapshot_contracts
COPY        config.py ./
COPY        app.py ./


#############################
### Build image for local ###
#############################
FROM        ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:${PARENT_IMAGE_TAG} AS local

WORKDIR     /var/task

LABEL       Version=local
ENV         build_version=local

COPY        --from=builder --chown=worker:worker ${LAMBDA_TASK_ROOT} ${LAMBDA_TASK_ROOT}

EXPOSE      8080
USER        worker

CMD         ["app.handler"]


##############################
### Build image for deploy ###
##############################
FROM        ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:${PARENT_IMAGE_TAG} AS deploy

ARG         GIT_COMMIT
ARG         REPOSITORY_URL
ARG         VERSION

WORKDIR     /var/task

LABEL       Version=$VERSION
ENV         build_version=$VERSION

COPY        --from=public.ecr.aws/datadog/lambda-extension:88 /opt/. /opt/

COPY        --from=builder --chown=worker:worker ${LAMBDA_TASK_ROOT} ${LAMBDA_TASK_ROOT}

EXPOSE      8080
USER        worker

ENV         DD_LAMBDA_HANDLER=app.handler

ENV         DD_GIT_COMMIT_SHA=$GIT_COMMIT
ENV         DD_GIT_REPOSITORY_URL=$REPOSITORY_URL
ENV         DD_VERSION=$VERSION

CMD         ["datadog_lambda.handler.handler"]
