# Run a round as a SERVICE ACCOUNT, not as a person.
#
# Why this exists, measured 2026-09-12. A corp Google account is refused by
# the Conversational Analytics API with `access_denied: Account restricted`,
# raised while minting the token, so no project or permission change reaches
# it. Four routes were tried and all four failed: the account directly, a
# second project, service-account impersonation (the corp identity is still
# what asks for the token), and a `gcert` refresh. The fifth, a downloaded
# service-account key, is blocked by org policy
# (`iam.disableServiceAccountKeyCreation`: `enforced: true`).
#
# On Cloud Run the machine IS the service account. Its metadata server hands
# the process a token with no login, no key on disk and no human identity in
# the chain. That is not a way around the restriction; it is how a batch job
# is supposed to authenticate.
#
# And it is the shape a customer needs anyway. A framework that only runs
# from one laptop with one person's credential cannot be handed to anybody.
ARG PYTHON_IMAGE=python:3.12.8-slim-bookworm
FROM ${PYTHON_IMAGE}

# Cloud extras only. The core is stdlib, and `dev` belongs in CI rather than
# in an image that runs paid rounds.
ENV PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

WORKDIR /app

# Pin installer and build backend for reproducible builds without pulling
# unpinned build-isolation tooling at build time.
RUN pip install --no-cache-dir --upgrade pip==25.0.1 hatchling==1.27.0

COPY pyproject.toml README.md ./
COPY src/ ./src/
RUN pip install --no-cache-dir --no-build-isolation \
    '.[bigquery,ca,catalog,yaml,gemini,lookml,duckdb]'

# Examples and helper scripts travel with the image so `contextlift` and
# `python scripts/...` run directly without requiring `make` or `uv`.
COPY examples/ ./examples/
COPY scripts/ ./scripts/

# Run as a dedicated non-root user with a writable /workspace mount point for
# local runs and Cloud Storage volume mounts.
RUN groupadd --system --gid 10001 contextlift \
    && useradd --system --uid 10001 --gid contextlift \
       --home-dir /home/contextlift --create-home --shell /usr/sbin/nologin contextlift \
    && mkdir -p /workspace \
    && chown -R contextlift:contextlift /app /workspace

USER contextlift

# Verify the installed CLI entry point executes cleanly as the non-root user.
RUN contextlift --help >/dev/null

# No ENTRYPOINT of its own: a Cloud Run job passes the whole command, so one
# image serves `round`, `classes`, `mine` and the free readers. Running the
# image with no arguments prints the verb list cleanly and exits 0.
CMD ["contextlift", "--help"]
