FROM public.ecr.aws/x8v8d7g8/mars-base:latest

WORKDIR /app

# Git time-travel: clone, then make the repo's default branch point AT the base
# commit with no future history — a real branch checkout (not a detached HEAD),
# future commits/tags gc'd away so the reference solution can't leak from history.
ARG BASE_SHA=954e5a474b9e3d45ca58f02a3a4eac8e1947acc5
RUN git clone https://github.com/sql-formatter-org/sql-formatter . \
 && DEFAULT="$(git remote show origin | sed -n 's/.*HEAD branch: //p')" \
 && git checkout -B "$DEFAULT" "$BASE_SHA" \
 && git remote remove origin \
 && for b in $(git for-each-ref --format='%(refname:short)' refs/heads | grep -vx "$DEFAULT"); do git branch -D "$b" || true; done \
 && for t in $(git tag); do git merge-base --is-ancestor "$t" HEAD 2>/dev/null || git tag -d "$t"; done \
 && git reflog expire --expire=now --all \
 && git gc --prune=now \
 && (git submodule update --init --recursive || true)

RUN NODE_ENV=development npm install --ignore-scripts && npx nearleyc src/parser/grammar.ne -o src/parser/grammar.ts

# v1.1 CTRF scoring: official CTRF jest reporter (github.com/ctrf-io/jest-ctrf-json-reporter,
# ctrf-io org), pinned and installed OUT-OF-TREE under /opt/jest-ctrf so /app stays
# byte-identical for the model.patch capture. jest-environment-node MUST be co-installed
# and pinned to the repo's jest version (29.7.0): 0.0.11's index.js loads dist/environment.js
# which hard-requires jest-environment-node at module load.
RUN mkdir -p /opt/jest-ctrf \
 && cd /opt/jest-ctrf \
 && npm install --no-audit --no-fund jest-ctrf-json-reporter@0.0.11 jest-environment-node@29.7.0 \
 && node -e "require('/opt/jest-ctrf/node_modules/jest-ctrf-json-reporter')" \
 && node -e "require.resolve('/opt/jest-ctrf/node_modules/jest-ctrf-json-reporter/dist/index.js')"

# The repo-level npm install above may rewrite the tracked yarn.lock and emit
# package-lock.json; restore a porcelain-clean worktree so Step 0 model.patch
# capture starts from zero diff, and assert cleanliness at build time.
RUN git checkout -- yarn.lock \
 && rm -f package-lock.json \
 && git status --porcelain \
 && test -z "$(git status --porcelain)"

CMD ["bash"]
