# syntax=docker/dockerfile:1.7

########################
### Build base image ###
########################
ARG         AWS_ACCOUNT=086679231553

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

USER root

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

# Setup env variables for Lambda runtime
ENV UV_LINK_MODE=copy \
    UV_PROJECT_ENVIRONMENT=/var/lang

WORKDIR /var/task

####################################
### Builder with dependencies    ###
####################################
FROM base AS builder

# Install build dependencies (only in builder stage)
RUN dnf -y install gcc && \
    dnf clean all

# Copy dependency files
COPY --link uv.lock pyproject.toml ./

# Install dependencies with dev group for testing
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=cache,target=/root/.cache/pip \
    uv sync --frozen --group dev --no-install-project

# Install qa-testing-utils
RUN --mount=type=cache,target=/root/.cache/uv \
    uv pip install --python /var/lang/bin/python3.13 qa-testing-utils

####################################
### Final test image (no gcc)    ###
####################################
FROM base AS deps

# Copy compiled dependencies from builder (without gcc)
COPY --from=builder /var/lang/lib/python3.13/site-packages /var/lang/lib/python3.13/site-packages
# Copy executables (ruff, pytest, etc.)
COPY --from=builder /var/lang/bin /var/lang/bin

##############################
### Build test image       ###
##############################
FROM deps AS test

# Copy application code and test files
COPY config.py app.py lint-and-test.sh docker-compose.yaml ./
COPY file_upload_complete/ ./file_upload_complete/
COPY tests/ ./tests/

# Copy dependency files
COPY --link uv.lock pyproject.toml ./

# Sync the project now that files are copied (for uv run to work)
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --group dev --no-install-project

# Fix permissions
RUN chmod -R 755 /var/task

ENTRYPOINT ["/usr/bin/env", "sh", "/var/task/lint-and-test.sh"]
