# Everything runs through uv; no need to activate a virtualenv by hand.
UV ?= uv
RUN := $(UV) run
NOTEBOOKS := notebooks

.DEFAULT_GOAL := help

.PHONY: help
help: ## Show this help
	@grep -hE '^[a-zA-Z_-]+:.*?## ' $(MAKEFILE_LIST) \
		| awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-16s\033[0m %s\n", $$1, $$2}'

.PHONY: install
install: ## Create the venv and install everything (runtime, notebook extras, dev tools)
	$(UV) sync --all-extras

.PHONY: lock
lock: ## Refresh uv.lock
	$(UV) lock

.PHONY: format
format: ## Format code and notebooks
	$(RUN) ruff format .
	$(RUN) ruff check --fix-only .

.PHONY: format-check
format-check: ## Check formatting without writing
	$(RUN) ruff format --check .

.PHONY: lint
lint: ## Lint code and notebooks
	$(RUN) ruff check .

.PHONY: typecheck
typecheck: ## Static type check with ty
	$(RUN) ty check

.PHONY: test
test: ## Run the offline test suite
	$(RUN) pytest

.PHONY: test-cov
test-cov: ## Run tests with a coverage report
	$(RUN) pytest --cov --cov-report=term-missing

.PHONY: test-network
test-network: ## Run the smoke tests that hit the real mempool.space
	$(RUN) pytest -m network

.PHONY: notebook-run
notebook-run: ## Execute every notebook top to bottom (hits the network)
	$(RUN) python scripts/run_notebooks.py $(NOTEBOOKS)

.PHONY: notebook
notebook: ## Open JupyterLab
	$(RUN) jupyter lab $(NOTEBOOKS)

.PHONY: check
check: format-check lint typecheck test ## Run every gate: format, lint, types, tests

.PHONY: clean
clean: ## Remove caches and build artifacts
	rm -rf .pytest_cache .ruff_cache .coverage htmlcov dist build
	find . -name __pycache__ -type d -prune -exec rm -rf {} +

.PHONY: slides
slides: ## Render the Marp deck to HTML (slides/deck.html)
	npx --yes @marp-team/marp-cli@4.1.2 --no-stdin --allow-local-files slides/deck.md -o slides/deck.html

.PHONY: slides-watch
slides-watch: ## Live-preview the Marp deck in the browser
	npx --yes @marp-team/marp-cli@4.1.2 --no-stdin --server slides/
