.PHONY: clean clean-test clean-pyc clean-build docs help lint format coverage \
        test run-tests run-examples install dist release servedocs check-dist
.DEFAULT_GOAL := help

PACKAGE  := statsinference
SRC      := src/$(PACKAGE)
DOCS_OUT := $(PACKAGE)-docs
# Where `make build-site` copies the docs (override: make build-site SITE_DIR=...)
SITE_DIR ?= ../../Pro_Website/Techtonique.github.io/$(PACKAGE)
# Use uv when available, plain pip otherwise
PIP := $(shell command -v uv >/dev/null 2>&1 && echo "uv pip" || echo "python3 -m pip")

define BROWSER_PYSCRIPT
import os, webbrowser, sys

from urllib.request import pathname2url

webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1])))
endef
export BROWSER_PYSCRIPT

define PRINT_HELP_PYSCRIPT
import re, sys

for line in sys.stdin:
	match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line)
	if match:
		target, help = match.groups()
		print("%-20s %s" % (target, help))
endef
export PRINT_HELP_PYSCRIPT

BROWSER := python3 -c "$$BROWSER_PYSCRIPT"

help:
	@python3 -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)

clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts

clean-build: ## remove build artifacts
	rm -fr build/
	rm -fr dist/
	rm -fr .eggs/
	find . -name '*.egg-info' -exec rm -fr {} +
	find . -name '*.egg' -exec rm -fr {} +

clean-pyc: ## remove Python file artifacts
	find . -name '*.pyc' -exec rm -f {} +
	find . -name '*.pyo' -exec rm -f {} +
	find . -name '*~' -exec rm -f {} +
	find . -name '__pycache__' -exec rm -fr {} +

clean-test: ## remove test and coverage artifacts
	rm -fr htmlcov .coverage .pytest_cache

lint: ## check style with flake8
	python3 -m flake8 $(SRC) tests examples --max-line-length=80 \
		--extend-ignore=E203,W503,E731

format: ## format code with black and autopep8 (line length 80)
	python3 -m black $(SRC) tests examples --line-length=80
	find $(SRC) -name "*.py" -exec python3 -m autopep8 --max-line-length=80 --in-place {} +

install: clean ## install the package (editable) with all extras
	$(PIP) install -e ".[all,test]"

test: ## run the tests (without reinstalling)
	python3 -m pytest -q tests

run-tests: install ## install, then run all the tests with coverage
	python3 -m coverage run --source=$(SRC) -m pytest -q tests
	python3 -m coverage report --show-missing

coverage: ## show the coverage report (after run-tests) and build htmlcov/
	python3 -m coverage report --show-missing
	python3 -m coverage html
	$(BROWSER) htmlcov/index.html

docs: install ## generate API docs with pdoc
	$(PIP) install pdoc
	python3 -m pdoc $(SRC) --output-dir $(DOCS_OUT)
	find . -name '__pycache__' -exec rm -fr {} +

servedocs: install ## serve the docs locally, reloading on change
	$(PIP) install pdoc
	python3 -m pdoc $(SRC)

build-site: docs ## copy the docs to the website folder (SITE_DIR)
	mkdir -p $(SITE_DIR)
	cp -rf $(DOCS_OUT)/* $(SITE_DIR)

run-examples: ## run all examples with one command
	find examples -maxdepth 2 -name "*.py" -exec python3 {} \;

dist: clean ## build source and wheel packages
	$(PIP) install build
	python3 -m build
	ls -l dist

check-dist: dist ## check the built packages render correctly on PyPI
	$(PIP) install twine
	python3 -m twine check dist/*

release: check-dist ## package and upload a release to PyPI
	python3 -m twine upload --repository pypi dist/* --verbose
