.PHONY: clean compile_translations coverage diff_cover docs dummy_translations \
        extract_translations fake_translations help pii_check pull_translations push_translations \
        quality requirements selfcheck test test-all upgrade validate install_transifex_client \
	produce_test_event consume_test_event multiple_consumer_test_event kill_all_consume_test_events \
	redis_up redis_down redis_shell

.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  %-30s\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

docs: ## generate Sphinx HTML documentation, including API docs
	uv sync --group doc
	rm -f docs/edx_event_bus_redis.rst
	rm -f docs/modules.rst
	DJANGO_SETTINGS_MODULE=test_settings PYTHONPATH=$(CURDIR) SPHINXOPTS=-W make -e -C docs clean
	DJANGO_SETTINGS_MODULE=test_settings PYTHONPATH=$(CURDIR) SPHINXOPTS=-W make -e -C docs html
	python -m build --wheel
	twine check dist/*
	$(BROWSER)docs/_build/html/index.html

upgrade: ## update the uv lockfile with the latest packages satisfying pyproject.toml
	uv run --with edx-lint edx_lint write_uv_constraints pyproject.toml
	uv lock --upgrade

quality: ## check coding style with pycodestyle and pylint
	uv sync --group quality
	touch tests/__init__.py
	PYTHONPATH=$(CURDIR) pylint src/edx_event_bus_redis tests test_utils manage.py
	rm tests/__init__.py
	PYTHONPATH=$(CURDIR) pycodestyle src/edx_event_bus_redis tests manage.py
	PYTHONPATH=$(CURDIR) pydocstyle src/edx_event_bus_redis tests manage.py
	PYTHONPATH=$(CURDIR) isort --check-only --diff tests test_utils src/edx_event_bus_redis manage.py test_settings.py
	$(MAKE) selfcheck

pii_check: ## check for PII annotations on all Django models
	DJANGO_SETTINGS_MODULE=test_settings code_annotations django_find_annotations --config_file .pii_annotations.yml --lint --report --coverage

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

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

validate: quality pii_check 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_event_bus_redis && ../../manage.py makemessages -l en -v1 -d django
	cd src/edx_event_bus_redis && ../../manage.py makemessages -l en -v1 -d djangojs

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

detect_changed_source_translations:
	cd src/edx_event_bus_redis && i18n_tool changed

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

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

dummy_translations: ## generate dummy translation (.po) files
	cd src/edx_event_bus_redis && 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

install_transifex_client: ## Install the Transifex client
	# Instaling client will skip CHANGELOG and LICENSE files from git changes
	# so remind the user to commit the change first before installing client.
	git diff -s --exit-code HEAD || { echo "Please commit changes first."; exit 1; }
	curl -o- https://raw.githubusercontent.com/transifex/cli/master/install.sh | bash
	git checkout -- LICENSE README.md ## overwritten by Transifex installer

## Local test helpers
produce_test_event: ## Produce a test event
	EVENT_BUS_REDIS_STREAM_MAX_LEN=5 EVENT_BUS_PRODUCER='edx_event_bus_redis.create_producer' EVENT_BUS_REDIS_CONNECTION_URL='redis://:password@localhost:6379/' EVENT_BUS_TOPIC_PREFIX='dev' python manage.py produce_event --signal openedx_events.content_authoring.signals.XBLOCK_DELETED --topic xblock-status --key-field None --data '{"xblock_info": {"usage_key": "block-v1:edx+DemoX+Demo_course+type@video+block@UaEBjyMjcLW65gaTXggB93WmvoxGAJa0JeHRrDThk", "block_type": "video"}}'

consume_test_event: ## Start consumer to consume test event
	EVENT_BUS_CONSUMER='edx_event_bus_redis.RedisEventConsumer' EVENT_BUS_PRODUCER='edx_event_bus_redis.create_producer' EVENT_BUS_REDIS_CONNECTION_URL='redis://:password@localhost:6379/' EVENT_BUS_TOPIC_PREFIX='dev' python manage.py consume_events --topic xblock-status --group_id test_group --extra '{"consumer_name": "test_group.c1"}'

multiple_consumer_test_event: ## Start 2 consumers to consume test event
	EVENT_BUS_CONSUMER='edx_event_bus_redis.RedisEventConsumer' EVENT_BUS_PRODUCER='edx_event_bus_redis.create_producer' EVENT_BUS_REDIS_CONNECTION_URL='redis://:password@localhost:6379/' EVENT_BUS_TOPIC_PREFIX='dev' python manage.py consume_events --topic xblock-status --group_id test_group --extra '{"consumer_name": "test_group.c1"}' &
	EVENT_BUS_CONSUMER='edx_event_bus_redis.RedisEventConsumer' EVENT_BUS_PRODUCER='edx_event_bus_redis.create_producer' EVENT_BUS_REDIS_CONNECTION_URL='redis://:password@localhost:6379/' EVENT_BUS_TOPIC_PREFIX='dev' python manage.py consume_events --topic xblock-status --group_id test_group --extra '{"consumer_name": "test_group.c2"}' &

kill_all_consume_test_events: ## Kill all test event consumers
	pgrep -lf python\ manage.py\ consume_events\ --topic\ xblock-status\ --group_id\ test_group | cut -d" " -f1 | xargs kill -15

redis_up: ## Start redis in docker container for testing locally.
	docker compose up

redis_down: ## Stop and remove redis container
	docker compose down

redis_shell: ## Start shell inside redis container
	docker compose exec redis /bin/bash -c "redis-cli -a password"
