###############################################################################
### Base image with uv installed and configured according to best practices ###
###############################################################################
FROM        086679231553.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:debian13-python314 AS base
ENV         UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy UV_PYTHON_DOWNLOADS=0
WORKDIR     /var/app
USER        root

RUN         apt-get -y update \
              && apt-get -y install git \
              && apt-get -y clean \
              && rm -rf /var/lib/apt/lists/*

# Allow Git commands to be run in mounted volumes where the owner UID does not match the container user.
USER        worker
RUN         git config --global --add safe.directory '*'

USER        root
COPY        --from=ghcr.io/astral-sh/uv:0.11.11 /uv /uvx /bin/

# Install syft for SBOM analysis
COPY        --from=anchore/syft:v1.44.0 /syft /usr/local/bin/syft

#################################
### Builder image for runtime ###
#################################
FROM        base as builder

COPY        uv.lock pyproject.toml ./

# Install dependencies with caching.
RUN         --mount=type=cache,target=/root/.cache/uv \
            uv sync --locked --no-install-project --no-dev

# Copy the rest of the project files and install it separately for optimal layer caching
COPY        ./datadog_tools ./datadog_tools
RUN         --mount=type=cache,target=/root/.cache/uv \
            uv sync --locked --no-dev

#####################
### Runtime image ###
#####################
FROM        base AS deploy
WORKDIR     /var/app
ENV         PATH="/var/app/.venv/bin:$PATH"
ENV         DD_ENV=prod
ENV         DD_SERVICE=datadog-tools

# Copy the application from the builder
COPY        --from=builder --chown=worker:worker /var/app /var/app

USER        worker
ENTRYPOINT  ["uv", "run", "--no-sync", "datadog-tools"]
CMD         ["--help"]

#######################
### Base dev image ###
#######################
FROM        base as base-dev

COPY        uv.lock pyproject.toml ./

# Install all dependencies, including dev dependencies
RUN         --mount=type=cache,target=/root/.cache/uv \
            uv sync --locked --no-install-project

# Copy the rest of the project files and install it separately for optimal layer caching
COPY        ./datadog_tools ./datadog_tools
RUN         --mount=type=cache,target=/root/.cache/uv \
            uv sync --locked

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

COPY        ./tests/unit ./tests/unit
COPY        ./scripts/unit-lint.sh ./scripts/unit-lint.sh

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

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

COPY        ./tests/integration ./tests
COPY        ./scripts/integration.sh ./scripts/integration.sh

ENTRYPOINT  ["scripts/integration.sh"]
