# Tier-1 e2e-runner image — multi-stage.
#
# Installs the gps-denied-onboard SUT so `gps-denied-replay` is on PATH for
# the tests in `tests/e2e/replay/`. Mirrors the install layout of
# `docker/companion-tier1.Dockerfile` minus the C++ build stage — the test
# runner only needs the Python entry points.
#
# Image layout intentionally mirrors the repo (so tests that compute
# `Path(__file__).resolve().parents[3] / "src" / "gps_denied_onboard" ...`
# resolve correctly):
#
#   /opt/pyproject.toml
#   /opt/src/gps_denied_onboard/...   (SUT package, editable install)
#   /opt/tests/...                    (bind-mounted from host)
#   /opt/_docs/00_problem/input_data/... (bind-mounted from host)
#
# Build context is the repo root (see `docker-compose.test.yml` →
# `services.e2e-runner.build.context`).

# Stage 1: system deps -------------------------------------------------------
FROM ubuntu:22.04 AS system-deps
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates \
        build-essential \
        libpq-dev \
        libspatialindex-dev \
        python3.10 \
        python3.10-venv \
        python3-pip \
    && rm -rf /var/lib/apt/lists/*

# Stage 2: python deps + SUT editable install -------------------------------
FROM system-deps AS python-deps
WORKDIR /opt
COPY pyproject.toml README.md ./
COPY src ./src
RUN python3 -m venv /opt/venv \
    && /opt/venv/bin/pip install --upgrade pip \
    && /opt/venv/bin/pip install --no-cache-dir -e ".[dev]"

# Stage 3: runtime ----------------------------------------------------------
FROM ubuntu:22.04 AS runtime
ARG DEBIAN_FRONTEND=noninteractive
# Notes on runtime deps beyond python:
#   * `python3`           — provides the /usr/bin/python3 symlink the venv's
#                            shebang resolves to; without it every
#                            console-script fails with "not found".
#   * `libgl1` + `libglib2.0-0` — opencv-python requires both at runtime.
#   * `libspatialindex-c6`     — rtree wrapper's native lib.
#   * `libpq5`                  — psycopg's libpq client.
RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates \
        python3 \
        python3.10 \
        libpq5 \
        libspatialindex-c6 \
        libgl1 \
        libglib2.0-0 \
    && rm -rf /var/lib/apt/lists/*
COPY --from=python-deps /opt/venv /opt/venv
COPY --from=python-deps /opt/src /opt/src
COPY --from=python-deps /opt/pyproject.toml /opt/pyproject.toml
ENV PATH="/opt/venv/bin:${PATH}"
ENV PYTHONPATH="/opt/src"
WORKDIR /opt
# `pytest /opt/tests/e2e/` exercises both `tests/e2e/replay/` (heavy
# replay tests gated by RUN_REPLAY_E2E) and any future `tests/e2e/scenarios/`
# additions. Rootdir resolves to /opt via the COPY'd pyproject.toml so
# `from tests.e2e.replay._helpers import ...` works inside the test files.
ENTRYPOINT ["pytest", "-v", "--tb=short", "/opt/tests/e2e/"]
