# Dockerfile

ARG         AWS_ACCOUNT=086679231553
ARG         UV_VERSION=0.11.19
ARG         PARENT_IMAGE_TAG=lambda-python311
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 \
            # 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" \
            UV_PROJECT_ENVIRONMENT=env

USER        root

WORKDIR     /var/task

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

# Build tooling to compile any sdists without a prebuilt wheel for this platform.
# unzip is used to extract the wkhtmltopdf bundle in the builder stage.
RUN         yum install -y gcc gcc-c++ make unzip


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

# Provide locale and cache settings for linting and test tooling executed via uv.
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

# 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

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


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

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

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


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

# 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/ ./src
COPY        config.py ./

# Extract the wkhtmltopdf binary, fonts and shared libraries used for PDF rendering.
COPY        ext/ /tmp/ext/
RUN         unzip /tmp/ext/wkhtmltox-0.12.6-4.amazonlinux2_lambda-modified.zip -d /opt/wkhtmltox

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

ARG         version
LABEL       Version=$version
ENV         build_version=$version

WORKDIR     /var/task

# Copy Datadog Lambda extension
COPY        --from=public.ecr.aws/datadog/lambda-extension:76 /opt/extensions/ /opt/extensions

# Copy the wkhtmltopdf binary, fonts and shared libraries for PDF rendering.
COPY        --from=builder /opt/wkhtmltox/ /opt/

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

# Snowflake connector needs a writable home for its configuration directory.
USER        root
RUN         mkdir -p /var/app/.snowflake && chmod 777 /var/app/.snowflake

EXPOSE      8080
USER        worker

# Set Lambda handler and PDF/Snowflake tooling environment variables
ENV         DD_LAMBDA_HANDLER=src.app.handler \
            LD_LIBRARY_PATH=/opt/lib \
            FONTCONFIG_PATH=/opt/fonts \
            SNOWFLAKE_HOME=/var/app/

CMD         ["datadog_lambda.handler.handler"]
