# 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:python314 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" \
    # Create virtualenv in a separate directory so that project files can be
    # mounted without overwriting the virtualenv when developing locally.
    UV_PROJECT_ENVIRONMENT="/opt/.venv"


ENV PATH="$UV_PROJECT_ENVIRONMENT/bin:$PATH"


USER        root

WORKDIR     /var/app


###########################
### 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        dev.py ./
COPY        ./sdlc_training_subbu ./sdlc_training_subbu

EXPOSE      5000
USER        worker

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

CMD         ["uv", "run", "ddtrace-run", "python", "dev.py", "2>&1"]


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

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

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


########################################
### Build image for integration-test ###
########################################
FROM        dev AS integration-test

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

ENTRYPOINT  ["scripts/integration.sh"]


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

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

# Install production dependencies.
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 sync --locked --no-install-project --no-dev

# Copy the rest of the project files
COPY        ./sdlc_training_subbu ./sdlc_training_subbu

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

ARG         GIT_COMMIT
ARG         REPOSITORY_URL
ARG         VERSION

# Copy the venv and application from the builder.
COPY        --from=builder --chown=worker:worker $UV_PROJECT_ENVIRONMENT $UV_PROJECT_ENVIRONMENT
COPY        --from=builder --chown=worker:worker /var/app /var/app

# Add uvicorn script for fastapi
COPY        uvicorn-start.sh  /

EXPOSE      8080
USER        worker

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

ENTRYPOINT  ["/uvicorn-start.sh"]
