.PHONY: help install-js install \
		update-npm-requirements static \
		extract_translations compile_translations generate_dummy_translations validate_translations \
		detect_changed_source_translations pull_translations push_translations check_translations_up_to_date \
		quality test-python render-templates test-js test-js-debug test test-acceptance test-a11y test-sandbox \
		requirements compile-requirements upgrade

.DEFAULT_GOAL := help

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

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

##################
# Install commands
##################

install-js: ## install JavaScript dependencies
	npm install

install: install-js static ## install all dependencies
	uv sync --group dev

requirements: ## sync the Python dev environment
	uv sync --group dev

compile-requirements: ## regenerate uv.lock from pyproject.toml
	uv lock

upgrade: ## upgrade all dependencies to the latest allowed versions
	uv run --with edx-lint edx_lint write_uv_constraints pyproject.toml
	uv lock --upgrade

##############################
# Generate js/css output files
##############################

STATIC_JS = src/openassessment/xblock/static/js
STATIC_CSS = src/openassessment/xblock/static/css

update-npm-requirements: ## update NPM requrements
	npm update --silent
	cp ./node_modules/backgrid/lib/backgrid*.js $(STATIC_JS)/lib/backgrid/
	cp ./node_modules/backgrid/lib/backgrid*.css $(STATIC_CSS)/lib/backgrid/

static: ## Webpack JavaScript and SASS source files
	npm run build

#######################
# Translations Handling
#######################

extract_translations: ## creates the django-partial.po & django-partial.mo files
	cd ./src/openassessment && django-admin makemessages -l en -v1 -d django
	cd ./src/openassessment && django-admin makemessages -l en -v1 -d djangojs

compile_translations: ## compiles the *.po & *.mo files
	cd ./src/openassessment/ && i18n_tool generate -v && cd ../..

generate_dummy_translations: ## generate dummy translations
	cd ./src/openassessment/ && i18n_tool dummy

validate_translations: ## Test translation files
	cd ./src/openassessment/ && i18n_tool validate -v

detect_changed_source_translations: ## check if translation files are up-to-date
	cd ./src/openassessment/ && i18n_tool changed

pull_translations: ## pull translations from Transifex
	tx pull -a -f -t --mode reviewed --minimum-perc=1

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

check_translations_up_to_date: extract_translations compile_translations generate_dummy_translations detect_changed_source_translations ## extract, compile, and check if translation files are up-to-date

##################
# Tests and checks
##################

quality: ## Run linting and code quality checks
	npm run lint
	./scripts/run-pycodestyle.sh
	./scripts/run-pylint.sh

test-python: ## Run Python tests
	uv run tox -e django52

render-templates: ## Render HTML templates
	./scripts/render-templates.sh

test-js: render-templates ## Run JavaScript frontend tests
	./scripts/test-js.sh

test-js-debug: render-templates ## Debug JavaScript tests using Karma
	./scripts/js-debugger.sh

test: quality test-python test-js ## Run quality checks and tests for Python and JavaScript

test-sandbox: test-acceptance test-a11y ## Run acceptance and accessibility tests

install-osx-requirements: ## Install OSX specific requirements using Homebrew
	brew install gettext
	brew link gettext --force

###################
# Devstack commands
###################

install-local-ora: ## installs your local ORA2 code into the LMS and Studio python virtualenvs
	docker exec -t edx.devstack.lms bash -c '. /edx/app/edxapp/venvs/edxapp/bin/activate && cd /edx/app/edxapp/edx-platform && pip uninstall -y ora2 && pip install -e /edx/src/edx-ora2 && pip freeze | grep ora2'
	docker exec -t edx.devstack.cms bash -c '. /edx/app/edxapp/venvs/edxapp/bin/activate && cd /edx/app/edxapp/edx-platform && pip uninstall -y ora2 && pip install -e /edx/src/edx-ora2 && pip freeze | grep ora2'
	docker exec -t edx.devstack.lms bash -c 'kill $$(ps aux | egrep "manage.py ?\w* runserver" | egrep -v "while|grep" | awk "{print \$$2}")'
	docker exec -t edx.devstack.cms bash -c 'kill $$(ps aux | egrep "manage.py ?\w* runserver" | egrep -v "while|grep" | awk "{print \$$2}")'

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
	# Load the PATH changes to make transifex client accessible from any directory i.e. openassessment for pull translations
	export PATH="$(PATH):$(pwd)"
	git checkout -- LICENSE ## overwritten by Transifex installer
	rm README.md ## pulled by Transifex installer
