# Makefile for Horus Runtime plugin development

# Command definitions (used in CI and locally)
PYTEST_CMD = pytest --cov=src/horus_slurm --cov-report=xml --cov-report=html --cov-report=term --junitxml=test-results.xml
RUFF_LINT_CMD = ruff check src/ tests/
RUFF_FORMAT_CHECK_CMD = ruff format --check --diff .
RUFF_FIX_CMD = ruff check --fix src/ tests/
RUFF_FORMAT_CMD = ruff format .
MYPY_CMD = mypy src/ tests/
INTEGRATION_CMD = pytest tests/integration --no-cov -p no:cacheprovider --junitxml=integration-results.xml

# Integration cluster (tests/integration/cluster)
ITEST_DIR ?= /tmp/horus-slurm-itest-$(shell id -u)
ITEST_PROJECT ?= horus-slurm-itest

# i18n settings (babel)
BABEL_CFG = babel.cfg
LOCALE_DIR = src/horus_slurm/locale
MESSAGES_POT = $(LOCALE_DIR)/messages.pot
DOMAIN = horus_slurm
SOURCE_DIR = src/horus_slurm

# Variables used for babel metadata
PROJECT_NAME = horus-slurm
ORGANIZATION = Temple Compute
LICENSE_TMPL = .mit.tmpl

.PHONY: test lint format type-check clean help ruff-check ruff-format-check mypy-check babel-update babel-check babel-add babel-extract add-license-headers integration integration-down

help:
	@echo "Available commands:"
	@echo "  test                Run all tests with coverage (same as CI)"
	@echo "  integration         Start the docker Slurm cluster and run integration tests"
	@echo "  integration-down    Remove the docker Slurm cluster"
	@echo "  lint                Run all linting tools (same as CI)"
	@echo "  ruff-check          Check code with ruff linter (used by CI)"
	@echo "  ruff-format-check   Check code formatting with ruff (used by CI)"
	@echo "  mypy-check          Check types with mypy (used by CI)"
	@echo "  format              Format code with ruff"
	@echo "  type-check          Run type checking"
	@echo "  babel-update        Update Babel translations"
	@echo "  babel-check         Check Babel translations (used by CI)"
	@echo "  babel-add           Add a new language (usage: make babel-add LANG=es)"
	@echo "  babel-extract       Extract translatable strings to messages.pot"
	@echo "  clean               Remove cache files"

test:
	# Set PYTHONPATH to current directory to ensure tests can find other test modules
	PYTHONPATH=. $(PYTEST_CMD)

integration:
	ITEST_DIR="$(ITEST_DIR)" ITEST_PROJECT="$(ITEST_PROJECT)" tests/integration/cluster/up.sh
	ITEST_DIR="$(ITEST_DIR)" HORUS_SLURM_ITEST=1 HORUS_SLURM_ITEST_LOGIN="$(ITEST_PROJECT)-slurmctld-1" PYTHONPATH=. $(INTEGRATION_CMD)

integration-down:
	ITEST_DIR="$(ITEST_DIR)" docker compose -p "$(ITEST_PROJECT)" -f tests/integration/cluster/compose.yaml down -v -t 2
	rm -rf "$(ITEST_DIR)"/test_*-*

# Individual check commands
ruff-check:
	$(RUFF_LINT_CMD)

ruff-format-check:
	$(RUFF_FORMAT_CHECK_CMD)

mypy-check:
	$(MYPY_CMD)

lint:
	$(RUFF_LINT_CMD)
	$(RUFF_FORMAT_CHECK_CMD)
	$(MYPY_CMD)

format:
	$(RUFF_FIX_CMD)
	$(RUFF_FORMAT_CMD)

type-check:
	$(MYPY_CMD)

add-license-headers:
	licenseheaders -t $(LICENSE_TMPL) -cy -o '$(ORGANIZATION)' -n $(PROJECT_NAME) -d src
	licenseheaders -t $(LICENSE_TMPL) -cy -o '$(ORGANIZATION)' -n $(PROJECT_NAME) -d tests

clean:
	find . -type d -name "__pycache__" -delete
	find . -type f -name "*.pyc" -delete
	rm -rf .coverage htmlcov/ .pytest_cache/
	rm -rf *.egg-info/ build/ dist/

babel-extract:
	pybabel extract -F $(BABEL_CFG) \
		--project=$(PROJECT_NAME) \
		--copyright-holder="$(ORGANIZATION)" \
		-o $(MESSAGES_POT) $(SOURCE_DIR)

babel-update:
	pybabel update -i $(MESSAGES_POT) -d $(LOCALE_DIR) -D $(DOMAIN) --no-fuzzy-matching

babel-refresh: babel-extract babel-update

babel-check:
	@echo "Checking for missing or fuzzy translations..."
	@for file in $(shell find $(LOCALE_DIR) -name "*.po"); do \
		RESULT=$$(msgfmt --statistics -c -o /dev/null $$file 2>&1); \
		echo "$$RESULT"; \
		if echo "$$RESULT" | grep -E "untranslated|fuzzy" > /dev/null; then \
			echo "ERROR: $$file has missing or fuzzy strings!"; \
			exit 1; \
		fi; \
	done
	@echo "Success: All strings are translated."
	pybabel compile -d $(LOCALE_DIR) -D $(DOMAIN) --statistics

babel-add:
	pybabel init -i $(MESSAGES_POT) -d $(LOCALE_DIR) -l $(LANG) -D $(DOMAIN)
