# Dockerfile

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

ENV         PYTHONUNBUFFERED=1 \
            # Configure optimizations recommended by https://docs.astral.sh/uv/guides/integration/docker/#optimizations
            UV_COMPILE_BYTECODE=1 \
            UV_LINK_MODE=copy \
            # Always use python from the base image
            UV_PYTHON_DOWNLOADS=never \
            # Configure uv cache to be independent of user home
            UV_CACHE_DIR="/tmp/uv_cache"

USER        root

WORKDIR     /var/task

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

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

# Install dev/test requirements
COPY        uv.lock pyproject.toml ./
RUN         --mount=type=cache,target=$UV_CACHE_DIR \
            uv sync --locked --no-install-project

# Copy the rest of the project files
COPY        config.py ./
COPY        src ./src

USER        worker

# Prevent unnecessary resyncs when mounting the project directory.
ENV         UV_NO_SYNC=1

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

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

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

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

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

# Install production dependencies. See https://docs.astral.sh/uv/guides/integration/aws-lambda/#deploying-a-docker-image
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}"

# Copy the rest of the project files
COPY        src ${LAMBDA_TASK_ROOT}/src
COPY        config.py ${LAMBDA_TASK_ROOT}/

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

ARG         GIT_COMMIT
ARG         REPOSITORY_URL
ARG         VERSION

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

# Copy the application and dependencies from the builder.
COPY        --from=builder --chown=worker:worker ${LAMBDA_TASK_ROOT} ${LAMBDA_TASK_ROOT}

EXPOSE      8080
USER        worker

# Set Lambda handler environment variable
ENV         DD_LAMBDA_HANDLER=src.app.handler

# Configure datadog environment variables
ENV         DD_GIT_COMMIT_SHA=$GIT_COMMIT
ENV         DD_GIT_REPOSITORY_URL=$REPOSITORY_URL
ENV         DD_VERSION=$VERSION

CMD         ["datadog_lambda.handler.handler"]
