# Web console for the competition data and outputs (Medical Imaging Explorer, Competition pages), one port:
# the built UI and the API (/api/...). Build context = the REPO ROOT:
#
#   docker build -f competition_models/platform/console/Dockerfile -t compmodels-console:local .
#
# Python: the console runs answer validation (compmodels.platform.check_answer) and mock scoring
# (mockdata/score_mock.py). Those need numpy / scipy / nibabel / pydicom but not torch, so this image installs
# Debian's python3 packages (~150 MB) and the compmodels source instead of basing on the 8.5 GB inference image.
# Runtime contract (env, volumes): platform/README.md "Console".
# apt: retries, no HTTP pipelining and up to three rounds, because the H100 server builds through a proxy that
# now and then answers 502 (platform/h100/deploy_h100.sh builds there).

# ---------------------------------------------------------------- 1. build the web app
FROM oven/bun:1.3.14 AS web
WORKDIR /src
COPY package.json bun.lock ./
COPY apps/server/package.json apps/server/
COPY apps/web/package.json apps/web/
COPY packages/shared/package.json packages/shared/
COPY packages/git-ui/package.json packages/git-ui/
COPY packages/TensorScope packages/TensorScope
RUN bun install --frozen-lockfile --ignore-scripts
COPY packages/shared packages/shared
COPY packages/git-ui packages/git-ui
COPY apps/web apps/web
RUN cd apps/web && bunx vite build

# ---------------------------------------------------------------- 2. server production deps
FROM oven/bun:1.3.14 AS deps
WORKDIR /app
COPY apps/server/package.json apps/server/
COPY packages/shared/package.json packages/shared/
# only the server's workspace: no web / viewer dependencies in the runtime image
RUN printf '{"name":"mie-console","private":true,"workspaces":["apps/server","packages/shared"]}\n' > package.json \
    && bun install --production --ignore-scripts --linker hoisted

# ---------------------------------------------------------------- test (not in the runtime image)
# The whole workspace with the dev dependencies, for the H100 test run (platform/h100/run_tests_h100.sh; the server
# has no internet, so everything the suites need is baked in):
#   docker build -f competition_models/platform/console/Dockerfile --target test -t compmodels-console-test:h100 .
#   docker run --rm compmodels-console-test:h100 bun run test:server   (test:web, typecheck, lint)
# Built only with --target test: the default build (the runtime stage below) never runs this stage.
FROM oven/bun:1.3.14 AS test
# git (gitRunner / code-clone tests make throwaway repos), python3 + the answer checker's packages (competition
# routes), curl / unzip / procps for the download and worker helpers the tests touch
RUN printf 'Acquire::Retries "8";\nAcquire::http::Pipeline-Depth "0";\n' > /etc/apt/apt.conf.d/80-flaky-proxy \
    && for i in 1 2 3; do apt-get update && apt-get install -y --no-install-recommends git python3 python3-numpy python3-yaml \
       python3-scipy python3-nibabel python3-pydicom ca-certificates curl unzip procps && break; \
       [ "$i" = 3 ] && exit 1; sleep 15; done \
    && rm -rf /var/lib/apt/lists/* \
    && git config --system user.name "mie-test" && git config --system user.email "mie-test@localhost" \
    && git config --system init.defaultBranch main && git config --system --add safe.directory '*'
WORKDIR /repo
COPY package.json bun.lock ./
COPY apps/server/package.json apps/server/
COPY apps/web/package.json apps/web/
COPY packages/shared/package.json packages/shared/
COPY packages/git-ui/package.json packages/git-ui/
COPY packages/TensorScope packages/TensorScope
RUN bun install --frozen-lockfile --ignore-scripts
COPY . .
ENV MIE_JUPYTER=0 PYTHONDONTWRITEBYTECODE=1 PYTHONPATH=/repo/competition_models/src
CMD ["sh", "-c", "bun run test:server && bun run test:web && bun run typecheck && bun run lint"]

# ---------------------------------------------------------------- 3. runtime
FROM oven/bun:1.3.14-slim
RUN printf 'Acquire::Retries "8";\nAcquire::http::Pipeline-Depth "0";\n' > /etc/apt/apt.conf.d/80-flaky-proxy \
    && for i in 1 2 3; do apt-get update && apt-get install -y --no-install-recommends python3 python3-numpy python3-yaml \
       python3-scipy python3-nibabel python3-pydicom ca-certificates curl && break; \
       [ "$i" = 3 ] && exit 1; sleep 15; done \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/package.json ./package.json
COPY apps/server/package.json apps/server/tsconfig.json apps/server/
COPY apps/server/src apps/server/src
COPY packages/shared packages/shared
COPY --from=web /src/apps/web/dist apps/web/dist
COPY README.md ./
COPY docs docs
# the competition package without mock data (mounted) and without the venv / caches (Dockerfile.dockerignore)
COPY competition_models competition_models

ENV MIE_HOST=0.0.0.0 \
    MIE_PORT=8787 \
    MIE_WORKSPACE_ROOT=/app \
    MIE_WEB_DIST=/app/apps/web/dist \
    MIE_MANAGED_ROOT=/var/lib/mie \
    MIE_DB_PATH=/var/lib/mie/db/explorer.sqlite \
    MIE_COMPETITION_ROOT=/app/competition_models \
    MIE_COMPETITION_PYTHON=/usr/bin/python3 \
    PYTHONPATH=/app/competition_models/src \
    PYTHONDONTWRITEBYTECODE=1 \
    MIE_COMP_MODE=auto \
    MIE_COMP_DATASET_ROOT=/2026aicompetition/datasets \
    MIE_COMP_TESTSET=/data/testset \
    MIE_COMP_WORKSPACE=/2026aicompetition/workspace \
    MIE_COMP_MODELS_ROOT=/LLM/models \
    MIE_JUPYTER=0
# the SQLite DB, the Settings-page files and the validation / score cache live here: mount a volume
VOLUME ["/var/lib/mie"]
# When this image was built (deploy_h100.sh passes it): the Docker page marks files newer than this as "modified on
# server". Last, so a new value only rebuilds this layer.
ARG CONSOLE_BUILT_AT=
ENV MIE_CONSOLE_BUILT_AT=${CONSOLE_BUILT_AT}
RUN python3 -c "import compmodels.platform.check_answer, compmodels.metrics.clinical, numpy, scipy, nibabel" \
    && mkdir -p /var/lib/mie
EXPOSE 8787
HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
    CMD curl -fsS "http://127.0.0.1:${MIE_PORT}/api/competition/location" >/dev/null || exit 1
WORKDIR /app/apps/server
CMD ["bun", "src/index.ts"]
