# Dockerfile

ARG         AWS_ACCOUNT=086679231553

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

COPY        --from=ghcr.io/astral-sh/uv:0.8.13 /uv /uvx /bin/

ENV         APP_DIR=/var/app \
            UV_FROZEN=1 \
            UV_LINK_MODE=copy \
            UV_NO_PROGRESS=1 \
            UV_PROJECT_ENVIRONMENT=/var/venv \
            VIRTUAL_ENV=/var/venv \
            PATH="/var/venv/bin:${PATH}"

USER        root
WORKDIR     ${APP_DIR}

RUN         --mount=type=cache,target=/var/cache/apt,sharing=locked \
            --mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
            apt-get -y update \
            && apt-get -y install --no-install-recommends \
                build-essential gcc libffi-dev zip \
            && rm -rf /var/lib/apt/lists/*

####################################
### Build image for dependencies ###
####################################
FROM        base AS deps

COPY        --link uv.lock pyproject.toml ${APP_DIR}/
RUN         --mount=type=cache,target=/root/.cache/uv \
            --mount=type=cache,target=/root/.cache/pip \
            uv sync --frozen --no-default-groups --no-install-project

##############################
### Build image for source ###
##############################
FROM        deps AS source

COPY        --link . ${APP_DIR}/

##############################
### Build images for venvs ###
##############################

### Production
FROM        source AS venv-prod

ENV         UV_COMPILE_BYTECODE=1
RUN         --mount=type=cache,target=/root/.cache/uv \
            --mount=type=cache,target=/root/.cache/pip \
            uv sync --frozen --no-default-groups

### Development
FROM        venv-prod AS venv-dev

ENV         UV_COMPILE_BYTECODE=0
RUN         --mount=type=cache,target=/root/.cache/uv \
            --mount=type=cache,target=/root/.cache/pip \
            uv sync --frozen --group dev

### Integration testing
FROM        venv-prod AS venv-integration

ENV         UV_COMPILE_BYTECODE=0
RUN         --mount=type=cache,target=/root/.cache/uv \
            --mount=type=cache,target=/root/.cache/pip \
            uv sync --frozen --group integration

#######################################
### Build image for live deployment ###
#######################################
FROM        venv-prod AS deploy

# Trim build tools in prod
RUN         --mount=type=cache,target=/var/cache/apt,sharing=locked \
            --mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
            apt-get purge -y gcc \
            && apt-get autoremove -y \
            && rm -rf /var/lib/apt/lists/*

# Remove tests, scripts, etc.
RUN         rm -rf \
            ${APP_DIR}/dev.py \
            ${APP_DIR}/scripts/ \
            ${APP_DIR}/tests/

# Entrypoint
COPY        --chmod=0755 uwsgi-start.sh /uwsgi-start.sh
USER        worker
ENTRYPOINT  ["uv", "run", "--frozen", "--no-sync", "--", "/uwsgi-start.sh"]
EXPOSE      8080

###########################
### Build image for dev ###
###########################
FROM        venv-dev AS dev

ENV         LC_ALL=en_US.utf-8 \
            LC_CTYPE=en_US.UTF-8 \
            PYTHONUNBUFFERED=1

CMD         ["sh", "-lc", "uv run --frozen --no-sync -- dev.py 2>&1"]

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

RUN         chmod +x ${APP_DIR}/scripts/unit-lint.sh
ENV         PYTHONDONTWRITEBYTECODE=1 \
            PYTHONPYCACHEPREFIX=/tmp/pycache \
            XDG_CACHE_HOME=/tmp/.cache \
            PYTEST_ADDOPTS=--basetemp=/tmp/pytest \
            COVERAGE_FILE=/tmp/.coverage
USER        worker

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

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

RUN         chmod +x ${APP_DIR}/scripts/test-integration.sh
ENV         PYTHONDONTWRITEBYTECODE=1 \
            PYTHONPYCACHEPREFIX=/tmp/pycache \
            XDG_CACHE_HOME=/tmp/.cache \
            PYTEST_ADDOPTS=--basetemp=/tmp/pytest \
            COVERAGE_FILE=/tmp/.coverage
USER        worker

ENTRYPOINT  ["scripts/test-integration.sh"]
