# Builder Stage
FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim AS builder

ENV UV_COMPILE_BYTECODE=1 \
    UV_LINK_MODE=copy \
    UV_PYTHON_DOWNLOADS=0

WORKDIR /app

# Install dependencies (without project) for optimal layer caching
RUN --mount=type=cache,target=/root/.cache/uv \
    --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

# Install project as a non-editable wheel into the venv
# --no-editable: source code is not required at runtime; only .venv is copied to the final image
COPY ./preference_center /app/preference_center

RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv sync --locked --no-dev --no-editable


# Deploy Stage
FROM python:3.14-slim-bookworm

# Install curl for ECS health checks
RUN apt-get update \
    && apt-get install -y --no-install-recommends curl \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN useradd -M -u 1000 app

# Copy only the virtual environment — source code stays in the builder
COPY --from=builder --chown=app:app /app/.venv /app/.venv
COPY --chmod=755 docker-entrypoint.sh /

USER app

ENV PATH="/app/.venv/bin:$PATH" \
    UVICORN_PORT=8080

EXPOSE 8080

ENTRYPOINT ["/docker-entrypoint.sh"]
