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

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

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    POETRY_VIRTUALENVS_IN_PROJECT=0 \
    POETRY_VIRTUALENVS_CREATE=0

USER worker

RUN pip install --no-cache-dir --upgrade pip setuptools && \
    pip install --no-cache-dir poetry==2.4.1

WORKDIR /var/task
COPY pyproject.toml ./

################################
### Generate poetry lockfile ###
################################
FROM base AS update-lockfile

COPY scripts/update-lockfile.sh ./scripts/
ENTRYPOINT ["scripts/update-lockfile.sh"]

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

ENV RUFF_CACHE_DIR=/tmp/.ruff_cache

COPY poetry.lock ./
RUN poetry install --no-root --no-cache --extras dev

COPY src ./src
COPY tests ./tests
COPY docker-compose.yml ./
COPY scripts/unit-lint.sh ./scripts/

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

##########################
### Format source code ###
##########################
FROM base AS format

COPY poetry.lock ./
RUN poetry install --no-root --no-cache --extras dev

COPY src ./src
COPY tests ./tests
COPY scripts/format.sh ./scripts/

ENTRYPOINT ["scripts/format.sh"]

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

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

COPY poetry.lock ./
RUN poetry install --only main --no-root --no-cache

COPY src ./src

ENV DD_LAMBDA_HANDLER=src.index.handler
CMD ["datadog_lambda.handler.handler"]
