.PHONY: clean compile-requirements compile_translations coverage diff_cover docs dummy_translations \
        extract_translations fake_translations help \
        quality requirements selfcheck test test-all upgrade validate

.DEFAULT_GOAL := help

# For opening files in a browser. Use like: $(BROWSER)relative/path/to/file.html
BROWSER := python -m webbrowser file://$(CURDIR)/

help: ## display this help message
	@echo "Please use \`make <target>' where <target> is one of"
	@awk -F ':.*?## ' '/^[a-zA-Z]/ && NF==2 {printf "\033[36m  %-25s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort

clean: ## remove generated byte code, coverage reports, and build artifacts
	find . -name '__pycache__' -exec rm -rf {} +
	find . -name '*.pyc' -exec rm -f {} +
	find . -name '*.pyo' -exec rm -f {} +
	find . -name '*~' -exec rm -f {} +
	rm -fr build/
	rm -fr dist/
	rm -fr *.egg-info

coverage: clean ## generate and view HTML coverage report
	DJANGO_SETTINGS_MODULE=test_settings pytest --cov-report html
	$(BROWSER)htmlcov/index.html

docs: ## generate Sphinx HTML documentation, including API docs
	doc8 --ignore-path docs/_build README.rst docs
	rm -f docs/edx_enterprise_subsidy_client.rst
	rm -f docs/modules.rst
	SPHINXOPTS="-W" make -e -C docs clean
	DJANGO_SETTINGS_MODULE=test_settings SPHINXOPTS="-W" make -e -C docs html
	$(BROWSER)docs/_build/html/index.html

compile-requirements: ## generate the uv.lock file without upgrading packages
	uv lock

upgrade: ## upgrade all packages in uv.lock and sync constraints from edx-lint
	uv run --with edx-lint edx_lint write_uv_constraints pyproject.toml
	uv lock --upgrade

quality: ## check coding style with pycodestyle and pylint
	touch tests/__init__.py
	pylint src/edx_enterprise_subsidy_client tests test_utils manage.py *.py
	rm tests/__init__.py
	pycodestyle src/edx_enterprise_subsidy_client tests  *.py
	isort --check-only --diff --recursive tests test_utils src/edx_enterprise_subsidy_client *.py test_settings.py
	python -m build --wheel
	twine check dist/*
	make selfcheck

pylint:
	touch tests/__init__.py
	pylint src/edx_enterprise_subsidy_client tests test_utils manage.py *.py

requirements: ## install development environment requirements
	uv sync --group dev

test: clean ## run tests in the current virtualenv
	DJANGO_SETTINGS_MODULE=test_settings pytest

diff_cover: test ## find diff lines that need test coverage
	diff-cover coverage.xml

test-all: quality ## run tests on every supported Python/Django combination
	tox

validate: quality test ## run tests and quality checks

isort-check:
	isort --check-only --diff --recursive tests test_utils src/edx_enterprise_subsidy_client *.py test_settings.py

isort:
	isort --recursive tests test_utils src/edx_enterprise_subsidy_client *.py test_settings.py

isort-fix:
	isort --recursive tests test_utils src/edx_enterprise_subsidy_client *.py test_settings.py

selfcheck: ## check that the Makefile is well-formed
	@echo "The Makefile is well-formed."
