APP := mapa

# The toolchain go.mod names, pinned here too: a newer local Go would
# otherwise win, and the gate would not be the one CI runs.
export GOTOOLCHAIN := go1.26.11

# estilo, the suite's design module, and notion, the Notion import's
# client, are private: fetch them with Git auth.
private_comma := ,
export GOPRIVATE := $(if $(GOPRIVATE),$(GOPRIVATE)$(private_comma))amadan.net/oficina/estilo.git,amadan.net/oficina/notion.git

# What `carlos ship -target` defaults to, and so what a release must be
# built for. Override for a one-off: make release RELEASE_GOARCH=amd64
RELEASE_GOOS   ?= linux
RELEASE_GOARCH ?= arm64
RELEASE_BIN := releases/$(APP)-$(RELEASE_GOOS)-$(RELEASE_GOARCH)

# What GET /api/version answers. An unstamped binary answers "dev"
# forever, and the deploy check can then never fail.
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null)

.PHONY: build release version-check deps test vet fmt-check code \
	migration-check mutate browser preview editor golden editor-golden \
	corpus-local credits credits-check ci locale-dates pandoc-fixture \
	spacing

build:
	CGO_ENABLED=0 go build ./...

release: version-check
	@mkdir -p releases
	CGO_ENABLED=0 GOOS=$(RELEASE_GOOS) GOARCH=$(RELEASE_GOARCH) go build -trimpath \
		-ldflags "-s -w -X amadan.net/rastrillo/rastrillo.BuildVersion=$(VERSION)" \
		-o $(RELEASE_BIN) ./cmd/$(APP)
	@echo "built $(RELEASE_BIN) reporting version $(VERSION)"

version-check:
	@test -n "$(VERSION)" || { echo "make release: no version to stamp; commit, or name it: make release VERSION=v0.1.0"; exit 1; }
	@case "$(VERSION)" in *-dirty) echo "make release: the tree is dirty, so $(VERSION) does not name what you would ship"; exit 1;; esac

# deps repairs a module cache whose extracted copy lost files (go mod
# verify names it), the CI runner's known failure (memoria's Makefile).
deps:
	@go mod download
	@out=$$(go mod verify 2>&1) || { \
		echo "$$out"; \
		echo "$$out" | sed -n 's/.*: dir has been modified (\(.*\))$$/\1/p' | while read -r dir; do \
			echo "deps: re-extracting $$dir"; chmod -R u+w "$$dir" && rm -rf "$$dir"; \
		done; \
		go mod download && go mod verify; \
	}

# test is every gate in spec §11 that slice 1 meets, the 10,000-page
# speed gate (6) included.
test:
	CGO_ENABLED=0 go test ./...

# Also vets the browser-tagged tests: they are outside `make test`, and a
# file behind a tag that nothing compiles rots silently (Fichas, once).
vet: deps
	go vet ./...
	go vet -tags browser ./internal/mapatest/

fmt-check:
	@out=$$(gofmt -l .); if [ -n "$$out" ]; then echo "gofmt needed:"; echo "$$out"; exit 1; fi

# code is rastrillo's code budget: 80 columns, 40-line functions,
# 400-line files, no one-letter names.
code:
	go tool rastrillo budget code

# spacing is Fiesta's spacing check (level-up build note 89): every
# margin, padding and gap in Mapa's own stylesheets is a step of the
# scale, var(--rst-sp-N), or says why it is not with
# /* spacing-allow: why */. estilo's and rastrillo's files are served
# from their modules, so nothing here is somebody else's to skip.
spacing:
	go tool fiesta spacing internal/mapa/static

# A step of its own, so ci.d/ can report it: it fails the build the
# moment models.go and migrations/ disagree, instead of at boot.
migration-check:
	go tool rastrillo migration check

# mutate breaks each security gate's code on purpose (spec §11: gates 1,
# 4 and 8; and L9's rename carry, level-up build note 79) through `go
# test -overlay`, never touching the tree, and fails unless every
# mutation makes its gate fail.
mutate:
	go run ./scripts/mutate

# browser is spec §11 gate 7: the walk in real Chromium, scripts off then
# on, failing on any console error. Behind a build tag because CI
# runners may have no Chromium. rastrillo's harness looks on PATH and in
# Linux's Playwright cache; on a Mac this finds Playwright's Chromium
# when RASTRILLO_CHROME is unset.
browser:
	@chrome="$${RASTRILLO_CHROME:-$$(ls -d "$$HOME"/Library/Caches/ms-playwright/chromium-*/chrome-mac*/*.app/Contents/MacOS/* 2>/dev/null | tail -1)}"; \
	RASTRILLO_CHROME="$$chrome" go test -tags browser -count=1 ./internal/mapatest/

# preview serves the real app, signed in, over synthetic pages on
# loopback: go run ./scripts/preview, then open http://127.0.0.1:8747/
preview:
	go run ./scripts/preview

# editor is the page editor's half of the gate (spec §6.2, §7): Node's
# dependencies exactly as the lockfile pins them, the bundle rebuilt and
# compared byte for byte with the committed one (L12, its size logged),
# then node --test, which holds markup.mjs and the schema's toDOM to Go's
# golden file (L4). It uses the node and npm on PATH. Drift is the
# rebuilt bundle against git's index (a staged bundle counts), and it is
# left in the tree for you to look at and commit; nothing is restored.
editor:
	@command -v node >/dev/null && command -v npm >/dev/null || { \
		echo "make editor: node and npm must be on PATH"; exit 1; }
	cd editor && npm ci --no-audit --no-fund --loglevel=error
	cd editor && node build-bundles.mjs
	@drift=$$(git diff --name-status -- 'internal/mapa/static/editor.*'; \
		git ls-files --others --exclude-standard -- 'internal/mapa/static/editor.*'); \
	if [ -n "$$drift" ]; then echo "$$drift"; \
		echo "make editor: internal/mapa/static/editor.*.js does not match a fresh build; commit the rebuilt bundle"; \
		exit 1; fi
	cd editor && node --test

# golden rewrites what gate L4 compares against: the corpus's synthetic
# pages, then markup's golden file, then the editor's (below). Commit all
# three.
golden:
	CGO_ENABLED=0 go test ./internal/mapa/ -run TestSyntheticCorpusIsCurrent -count=1 -update
	CGO_ENABLED=0 go test ./internal/mapa/markup/ -run 'TestGolden$$|TestGoldenLocales$$' -count=1 -update
	CGO_ENABLED=0 go test ./internal/mapa/locale/ -run 'TestDateLabelsAreCurrent$$' -count=1 -update
	$(MAKE) editor-golden

# pandoc-fixture records how the pandoc on PATH reads every column layout
# case and Join's output for it (columns and icons spec COL12), which the
# markup tests hold Mapa to. CI has no pandoc; commit the result.
pandoc-fixture:
	CGO_ENABLED=0 go test ./internal/mapa/markup/ -run 'TestCOL12' -count=1 -pandoc

# locale-dates rewrites internal/mapa/locale/dates.json, the CLDR date
# patterns Mapa writes dates with (level-up build note 85), from the ICU
# the node on PATH links, then the cases Go and the editor are held to.
# Commit both. A different Node may carry a different CLDR: the file
# records which one it came from.
locale-dates:
	node editor/gen-dates.mjs
	CGO_ENABLED=0 go test ./internal/mapa/locale/ -run 'TestDateLabelsAreCurrent$$' -count=1 -update

# editor-golden rewrites markup/testdata/editor.json, gates L1 and L3's Go
# half: the bodies the editor's seeded edits saved, with the blocks its
# document held, which markup's TestEditorBodies parses with Go.
editor-golden:
	cd editor && npm ci --no-audit --no-fund --loglevel=error
	cd editor && MAPA_EDITOR_UPDATE=1 node --test fidelity.test.mjs

# corpus-local runs gates L1 to L4 over Markdown read in place, for example
# make corpus-local DIR=/abs/path/to/notes. Go's answers go to a
# temporary file outside the repository, removed afterwards: nothing
# read is committed. Record the result in the build notes.
corpus-local:
	@test -n "$(DIR)" || { echo "usage: make corpus-local DIR=<a directory of .md files>"; exit 1; }
	@out=$$(mktemp -t mapa-corpus); \
	MAPA_CORPUS_DIR="$(DIR)" MAPA_CORPUS_OUT="$$out" CGO_ENABLED=0 \
		go test ./internal/mapa/markup/ -run TestCorpusLocal -count=1 -v && \
	(cd editor && MAPA_GOLDEN="$$out" node --test parity.test.mjs fidelity.test.mjs); \
	status=$$?; rm -f "$$out"; exit $$status

# credits regenerates Help → Colophon's inventory,
# internal/mapa/credits/: the Go modules the release build links (estilo's
# oficina-notices over ./cmd/mapa for linux/arm64), Lucide's notice, and
# the npm packages inside the committed editor bundle, each with its own
# licence text. It runs npm ci in editor/, so it needs node and npm on
# PATH. Commit both files. credits-check needs Go only: it fails when the
# Go modules, their notices, the toolchain, the lockfile or the bundle no
# longer match what was recorded.
credits:
	python3 scripts/credits.py

credits-check:
	python3 scripts/credits.py --check

# ci is the one gate: what the runner executes and what you run before
# pushing.
ci: vet fmt-check code spacing test migration-check editor credits-check
