.PHONY: help
help:             ## Show the help.
	@echo "Usage: make <target>"
	@echo ""
	@echo "Targets:"
	@fgrep "##" Makefile | fgrep -v fgrep

.PHONY: show
show:             ## Show the current environment.
	@echo "Current environment:"
	@uv run python -V
	@uv run python -m site

.PHONY: install
install:          ## Install the project in dev mode.
	uv sync

.PHONY: install-hooks
install-hooks:    ## Install git pre-commit hooks.
	cp scripts/pre-commit .git/hooks/pre-commit
	chmod +x .git/hooks/pre-commit
	@echo "Pre-commit hook installed."

.PHONY: fmt
fmt:              ## Format code using ruff (whole repo, including Python in Markdown).
	uv run ruff format .
	uv run ruff check --fix .

.PHONY: lint
lint:             ## Run ruff and ty linters.
	uv run ruff check .
	uv run ruff format --check .
	uvx ty check pcons/ examples/

.PHONY: lint-rez
lint-rez:         ## Type-check the rez integration (needs rez installed).
	find pcons/integrations/rez -name '*.py' -print0 | xargs -0 uvx ty check

.PHONY: test
test:             ## Run tests.
	uv run pytest -n auto

.PHONY: fuzz
fuzz:             ## Run the property tests in a long campaign (as CI does nightly).
	uv run pytest tests/fuzz -m fuzz --hypothesis-profile=nightly

.PHONY: test-cov
test-cov:         ## Run tests with coverage report.
	uv run pytest --cov=pcons --cov-branch --cov-report=html --cov-report=xml
	@echo "Coverage report: htmlcov/index.html"

.PHONY: test-cov-main-diff
test-cov-main-diff: test-cov   ## Show coverage diff with main branch.
	uvx diff-cover coverage.xml --compare-branch=origin/main \
		--format html:htmlcov/main-diff-cover.html \
		--format markdown:htmlcov/main-diff-cover.md
	@echo "Coverage diff report: htmlcov/main-diff-cover.html and htmlcov/main-diff-cover.md"

.PHONY: watch
watch:            ## Run tests on every change.
	ls pcons/**/*.py tests/**/*.py | entr uv run pytest -x

.PHONY: clean
clean:            ## Clean unused files.
	@find ./ -name '*.pyc' -exec rm -f {} \;
	@find ./ -name '__pycache__' -exec rm -rf {} \;
	@find ./ -name 'Thumbs.db' -exec rm -f {} \;
	@find ./ -name '*~' -exec rm -f {} \;
	@rm -rf .cache
	@rm -rf .pytest_cache
	@rm -rf .mypy_cache
	@rm -rf .ruff_cache
	@rm -rf build
	@rm -rf dist
	@rm -rf *.egg-info
	@rm -rf htmlcov
	@rm -rf .tox/
	@rm -rf docs/_build

.PHONY: docs
docs:             ## Build the single-page dogfood demo (pcons building its own front page). The real site is `make docs-site`.
	@echo "building dogfood demo page (docs/pcons-build.py) ..."
	uv run pcons -C docs
	@open docs/build/index.html || xdg-open docs/build/index.html

.PHONY: docs-site
docs-site:        ## Build and serve the documentation site (mkdocs — what ReadTheDocs publishes).
	uvx --with mkdocs-material --with mkdocs-macros-plugin mkdocs serve
