.PHONY: build_docs build_dummy_translations clean compile_translations \
        coverage detect_changed_source_translations diff_cover docs \
        dummy_translations extract_translations help isort isort_check \
        pull_translations push_translations pylint quality \
        requirements selfcheck style test upgrade \
        validate validate_translations

.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 {} +
	coverage erase
	rm -fr build/
	rm -fr dist/
	rm -fr *.egg-info

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

build_docs:
	doc8 --ignore-path docs/_build README.rst docs
	make -C docs clean
	make -C docs html
	python -m build --sdist
	twine check dist/*.tar.gz

docs: build_docs ## generate and show Sphinx HTML documentation, including API docs
	$(BROWSER)docs/_build/html/index.html

upgrade: ## update the requirements in pyproject.toml (uv.lock)
	uv run --with 'edx-lint>=6.2' edx_lint write_uv_constraints pyproject.toml
	uv lock --upgrade

CHECKABLE_PYTHON=tests test_utils example src/edx_api_doc_tools manage.py test_settings.py

style:
	pycodestyle $(CHECKABLE_PYTHON)
	pydocstyle $(CHECKABLE_PYTHON)

isort:  ## sort all imports
	isort --recursive $(CHECKABLE_PYTHON)

isort_check:
	isort --check-only --diff $(CHECKABLE_PYTHON)

pylint:
	echo '"""This file exists only to satisify pylint; it is not committed."""' > tests/__init__.py
	pylint --django-settings-module=test_settings $(CHECKABLE_PYTHON)
	rm tests/__init__.py

quality: style isort_check pylint ## check code style, import ordering, linting, and this makefile
	make selfcheck

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

test: clean ## run tests in the current virtualenv
	pytest

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

validate: quality test ## run tests and quality checks

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

## Localization targets

extract_translations: ## extract strings to be translated, outputting .mo files
	rm -rf docs/_build
	cd src/edx_api_doc_tools && python ../../manage.py makemessages -l en -v1 -d django
	cd src/edx_api_doc_tools && python ../../manage.py makemessages -l en -v1 -d djangojs

compile_translations: ## compile translation files, outputting .po files for each supported language
	cd src/edx_api_doc_tools && python ../../manage.py compilemessages

detect_changed_source_translations:
	cd src/edx_api_doc_tools && i18n_tool changed

pull_translations: ## pull translations from Transifex
	tx pull -t -a -f --mode reviewed

push_translations: ## push source translation files (.po) from Transifex
	tx push -s

dummy_translations: ## generate dummy translation (.po) files
	cd src/edx_api_doc_tools && i18n_tool dummy

build_dummy_translations: extract_translations dummy_translations compile_translations ## generate and compile dummy translation files

validate_translations: build_dummy_translations detect_changed_source_translations ## validate translations
