.DEFAULT_GOAL := help
VENV ?= venv
PYTHON ?= python3
UV ?= uv
UV_VERSION := $(shell $(PYTHON) scripts/check_toolchain.py --print-version)
NODE ?= node
VHS ?= vhs
DEMO_DIR ?= output/reconcile-demo
DOCKER ?= docker
DIST_DIR ?= dist
FLY ?= flyctl
GH ?= gh
ACTIONLINT ?= $(CURDIR)/.tools/bin/actionlint
ACTIONLINT_VERSION := $(shell awk '$$1 == "actionlint" {print $$2}' .tool-versions)
PY := $(VENV)/bin/python
PIP := $(VENV)/bin/pip
CLI := $(VENV)/bin/treestamp

.PHONY: help venv install format format-check lint typecheck test cov ci build distribution-check integration-check hook-check pre-commit-check tui clean web-install web-lint web-typecheck web-test web-cov web-ci web-fake web-serve web-image web-js-test web-browser-test fly-secrets fly-deploy fly-status fly-logs fly-smoke docs-check metadata-check
GITHUB_API_URL ?= http://127.0.0.1:8100
FAKE_PORT ?= 8100
FAKE_FLAGS ?=
FLY_CONFIG ?= web/fly.toml
FLY_PRODUCTION_CONFIG ?= web/fly.toml
FLY_PREVIEW_CONFIG ?= web/fly.preview.toml
FLY_DEPLOY_FLAGS ?=
FLY_HA ?= true
BUILD_COMMIT ?= $(shell git rev-parse HEAD)
SMOKE_REPO ?= reponomics/treestamp
SMOKE_URL ?= https://treestamp.reponomics.org
SMOKE_CHECKOUT ?= .
WEB_VENV := $(abspath $(VENV))
UI_URL ?= http://127.0.0.1:8000
UI_REPO ?= fixture/repo
UI_OUTPUT ?= output/ui-review
UI_BROWSER ?= chrome
# Extra pytest-cov options for cov and web-cov, e.g. COV_ARGS=--cov-report=xml
COV_ARGS ?=
# Fail on locale-dependent text I/O, including in Python subprocesses.
test cov web-test web-cov web-browser-test toolchain-check: export PYTHONWARNDEFAULTENCODING = 1
test cov web-test web-cov web-browser-test toolchain-check: export PYTHONWARNINGS := $(PYTHONWARNINGS),error::EncodingWarning
.PHONY: tls-check fly-production-deploy fly-preview-deploy workflow-check lock-check lock-update web-browser-install uv-check toolchain-check
export UV_PROJECT_ENVIRONMENT := $(abspath $(VENV))
help: ## Show available targets
	@awk '/^[a-zA-Z_-]+(\\:[a-zA-Z_-]+)?:.*## / {name = $$0; sub(/: .*/, "", name); gsub(/\\:/, ":", name); description = $$0; sub(/.*## /, "", description); printf "%-22s %s\n", name, description}' $(MAKEFILE_LIST)
.PHONY: check\:pre-commit check\:pr _check-pr-core _check-pr-web
check\:pre-commit: ## Before committing: run development hooks on staged files
	$(VENV)/bin/pre-commit run
check\:pr: ## Before a PR: run local CI checks, package acceptance, and container build
	$(MAKE) _check-pr-core
	$(MAKE) _check-pr-web
# Shared CI phases; keep the public checkpoints above as the developer entry points.
# The core phase syncs the environment, so finish it before starting hosted tests.
_check-pr-core:
	$(MAKE) toolchain-check lock-check workflow-check
	@set -eu; check_dist=$$(mktemp -d "$${TMPDIR:-/tmp}/treestamp-check.XXXXXX"); \
	  trap 'rm -rf "$$check_dist"' EXIT; \
	  $(MAKE) build DIST_DIR="$$check_dist"; \
	  $(MAKE) ci docs-check integration-check metadata-check distribution-check hook-check DIST_DIR="$$check_dist"
# Docker build steps change the host's network interfaces, which makes Chromium
# abort in-flight requests (ERR_NETWORK_CHANGED), so build the image only after
# the browser journeys finish.
_check-pr-web: web-ci web-browser-test
	$(MAKE) web-image
toolchain-check: ## Verify the uv pin in web/Dockerfile, the only uv pin
	$(PYTHON) scripts/check_toolchain.py
uv-check: ## Check that uv matches the pinned development tool version
	@test "$$($(UV) --version | awk '{print $$2}')" = "$(UV_VERSION)" || \
	  { echo "Install uv $(UV_VERSION), as pinned in web/Dockerfile."; exit 1; }
venv: uv-check ## Create the virtual environment
	@test -d "$(VENV)" || $(UV) venv --python $(PYTHON) $(VENV)
lock-check: uv-check ## Reject dependency declarations that differ from the lock
	$(UV) lock --check
lock-update: uv-check ## Resolve intentional dependency upgrades into uv.lock
	$(UV) lock --upgrade
install: uv-check ## Sync locked core, hosted, build, and test dependencies into venv
	$(UV) sync --locked --all-packages --all-extras --no-install-workspace --no-build
	$(UV) sync --locked --all-packages --all-extras --no-build-isolation
format: ## Format Python code
	$(VENV)/bin/ruff format .
format-check: ## Check Python formatting
	$(VENV)/bin/ruff format --check .
lint: ## Lint Python code
	$(VENV)/bin/ruff check .
typecheck: ## Check strict Python types
	$(VENV)/bin/mypy
test: ## Run the regression suite, including headless TUI journeys
	$(PY) -m pytest
cov: ## Measure core Python statement and branch coverage (95% minimum)
	$(PY) -m pytest --cov --cov-report=term-missing --cov-fail-under=95 $(COV_ARGS)
.PHONY: cov-python cov-all
cov-python: ## Run both Python coverage suites and report their combined total
	rm -f .coverage.python
	$(MAKE) cov COVERAGE_FILE="$(CURDIR)/.coverage"
	$(MAKE) web-cov COVERAGE_FILE="$(CURDIR)/web/.coverage"
	$(PY) -m coverage combine --data-file=.coverage.python --keep .coverage web/.coverage
	$(PY) -m coverage report --data-file=.coverage.python --fail-under=95
cov-all: cov-python web-js-cov ## Measure all current coverage scopes: combined Python and separate JavaScript
ci: format-check lint typecheck cov ## Run core formatting, lint, types, and coverage checks
docs-check: ## Check retained local documentation links and packaged README URLs
	$(PY) scripts/check_docs.py
metadata-check: ## Check built metadata, README rendering, package contents, and hashes
	$(PY) -m twine check --strict "$(DIST_DIR)"/*.whl "$(DIST_DIR)"/*.tar.gz
	$(PY) scripts/release_check.py --dist "$(DIST_DIR)"
build: install ## Sync locked build tools and build wheel and sdist without isolation
	$(PY) -m build --no-isolation --outdir "$(DIST_DIR)"
distribution-check: ## Verify clean wheel/sdist headless commands and editor workflows
	$(PY) scripts/distribution_check.py --dist "$(DIST_DIR)"
.PHONY: readme-styles-check readme-update
readme-styles-check: ## Verify alternate README styles match its managed tree
	$(PY) scripts/readme_styles.py
readme-update: ## Refresh the managed README tree and all style previews
	$(CLI) update README.md
	$(PY) scripts/readme_styles.py --write
integration-check: ## Verify the README tree and all alternate style previews
	$(CLI) check README.md
	$(PY) scripts/readme_styles.py
.PHONY: actionlint-install actionlint-check
actionlint-install: ## Install pinned actionlint after verifying its release attestation (requires gh)
	sh scripts/install_actionlint.sh "$(ACTIONLINT_VERSION)" "$(ACTIONLINT)" "$(GH)"
actionlint-check: ## Require the actionlint version pinned in .tool-versions
	@version=$$("$(ACTIONLINT)" -version) && \
	  test "$$(printf '%s\n' "$$version" | awk 'NR == 1 {sub(/^v/, ""); print; exit}')" = "$(ACTIONLINT_VERSION)" || \
	  { echo "Run make actionlint-install for pinned actionlint $(ACTIONLINT_VERSION)." >&2; exit 1; }
workflow-check: actionlint-check ## Validate GitHub Actions workflows with pinned actionlint
	"$(ACTIONLINT)"
hook-check: ## Exercise installed repository hooks at HEAD
	$(PY) scripts/hook_check.py
pre-commit-check: ## Run the repository development hooks
	$(VENV)/bin/pre-commit run --all-files
.PHONY: reconcile-demo reconcile-video
reconcile-demo: ## Try annotation placement in a disposable example repository
	PYTHONPATH=src $(PY) scripts/reconcile_demo.py

reconcile-video: ## Record and verify a TUI note-placement demo (requires VHS)
	PYTHONPATH=src $(PY) scripts/demos/record_reconcile.py --vhs "$(VHS)" --output "$(DEMO_DIR)"

tui: ## Open the editor
	$(CLI)
clean: ## Remove generated build and coverage artifacts
	rm -rf dist/ build/ .coverage .coverage.python htmlcov/ coverage.xml web/.coverage web/coverage.xml
web-install: install ## Sync the locked workspace, including hosted development dependencies
web-browser-install: install ## Sync the locked browser test dependencies (install Chromium separately)
web-lint: ## Lint the web editor project
	$(VENV)/bin/ruff check web && $(VENV)/bin/ruff format --check web
web-typecheck: ## Check strict types for the web editor project
	cd web && $(WEB_VENV)/bin/mypy
web-test: ## Run the web editor tests
	cd web && $(WEB_VENV)/bin/python -m pytest
web-cov: ## Measure hosted Python statement and branch coverage (95% minimum)
	cd web && $(WEB_VENV)/bin/python -m pytest --cov --cov-report=term-missing --cov-fail-under=95 $(COV_ARGS)
web-ci: web-lint web-typecheck web-cov web-js-cov ## Run hosted Python and JS quality gates (browser tests are separate)
web-js-test: ## Run the page's pure-logic tests with Node's test runner
	$(NODE) --test web/tests/js/*.test.mjs
.PHONY: web-js-cov
web-js-cov: ## Measure pure JavaScript logic coverage (excludes browser DOM code)
	$(NODE) --experimental-test-coverage --test --test-coverage-include='**/static/logic.js' web/tests/js/*.test.mjs
web-browser-test: ## Drive the page in a browser (needs make install and a browser)
	cd web && $(WEB_VENV)/bin/python -m pytest -q -o addopts="" -m browser tests/browser $(BROWSER_FLAGS)
.PHONY: web-ui-screenshots web-ui-clean
web-ui-screenshots: ## Capture UI states and contact sheets for a quick visual review
	$(PY) web/scripts/capture_ui.py --url "$(UI_URL)" --repo "$(UI_REPO)" --output "$(UI_OUTPUT)" --browser "$(UI_BROWSER)"
web-ui-clean: ## Remove the screenshot gallery after review
	$(PY) web/scripts/capture_ui.py --output "$(UI_OUTPUT)" --clean
web-fake: ## Serve this checkout as a fake GitHub API on FAKE_PORT (FAKE_FLAGS adds failure modes)
	$(PY) web/scripts/fake_github.py --repo . --port $(FAKE_PORT) $(FAKE_FLAGS)
web-serve: ## Run the web editor locally against GITHUB_API_URL
	GITHUB_API_URL=$(GITHUB_API_URL) GITHUB_RATE_RESERVE=0 $(VENV)/bin/uvicorn --factory treestamp_web.app:create_app --host 127.0.0.1 --port 8000 --reload
tls-check: ## Check Python TLS package versions and certificate trust stores
	$(PY) web/scripts/check_tls.py
web-image: toolchain-check ## Build the web editor container image
	$(DOCKER) build -f web/Dockerfile --build-arg BUILD_COMMIT="$(BUILD_COMMIT)" -t treestamp-web:local .
fly-secrets: ## Set the GitHub App secrets on Fly (GITHUB_APP_CLIENT_ID, GITHUB_APP_INSTALLATION_ID, GITHUB_APP_PRIVATE_KEY_FILE)
	@test -n "$(GITHUB_APP_CLIENT_ID)" -a -n "$(GITHUB_APP_INSTALLATION_ID)" -a -n "$(GITHUB_APP_PRIVATE_KEY_FILE)" || \
	  { echo "usage: make fly-secrets GITHUB_APP_CLIENT_ID=... GITHUB_APP_INSTALLATION_ID=... GITHUB_APP_PRIVATE_KEY_FILE=path.pem"; exit 2; }
	$(FLY) secrets set --config $(FLY_CONFIG) GITHUB_APP_CLIENT_ID="$(GITHUB_APP_CLIENT_ID)" \
	  GITHUB_APP_INSTALLATION_ID="$(GITHUB_APP_INSTALLATION_ID)" GITHUB_APP_PRIVATE_KEY="$$(cat $(GITHUB_APP_PRIVATE_KEY_FILE))"
fly-deploy: ## Build and deploy the app selected by FLY_CONFIG (defaults to production)
	$(FLY) deploy --config $(FLY_CONFIG) --dockerfile web/Dockerfile --build-arg BUILD_COMMIT="$(BUILD_COMMIT)" --ha=$(FLY_HA) $(FLY_DEPLOY_FLAGS)
fly-production-deploy: ## Build and deploy the production editor
	$(MAKE) fly-deploy FLY_CONFIG=$(FLY_PRODUCTION_CONFIG) FLY_HA=true
fly-preview-deploy: ## Build and deploy the separate test editor
	$(MAKE) fly-deploy FLY_CONFIG=$(FLY_PREVIEW_CONFIG) FLY_HA=false
fly-status: ## Show the app's machines and health
	$(FLY) status --config $(FLY_CONFIG)
fly-logs: ## Stream the service's log lines from Fly
	$(FLY) logs --config $(FLY_CONFIG)
fly-smoke: ## Run the smoke script against the deployed app (SMOKE_REPO, SMOKE_CHECKOUT)
	$(PY) web/scripts/smoke.py $(SMOKE_URL) --repo $(SMOKE_REPO) --checkout $(SMOKE_CHECKOUT)
