include i18n.mk

# Makefile variables set automatically
plugin_id=`cat plugin.json | python -c "import sys, json; print(str(json.load(sys.stdin)['id']).replace('/',''))"`
plugin_version=`cat plugin.json | python -c "import sys, json; print(str(json.load(sys.stdin)['version']).replace('/',''))"`
archive_file_name="dss-plugin-${plugin_id}-${plugin_version}.zip"
remote_url=`git config --get remote.origin.url`
last_commit_id=`git rev-parse HEAD`
ifneq ($(wildcard .venv/bin/python),)
PYTHON_BIN := $(abspath .venv/bin/python)
else
PYTHON_BIN := python3
endif
BACKEND_PYTHON_PATHS := python-lib/backend python-lib/export_recipe tests/unit

.DEFAULT_GOAL := plugin

plugin: check-remaining-flags-in-translations check-translations-consistency dist-clean
	@echo "[START] Archiving plugin to dist/ folder..."
	@cat plugin.json | json_pp > /dev/null
	@mkdir dist
	@echo "{\"remote_url\":\"${remote_url}\",\"last_commit_id\":\"${last_commit_id}\"}" > release_info.json
	@git archive -v -9 --format zip -o dist/${archive_file_name} HEAD
	@if [[ -d tests ]]; then \
		zip --delete dist/${archive_file_name} "tests/*"; \
	fi
	@zip --delete dist/${archive_file_name} "docs/*" "scripts/*" "dev-*" "dev.*" "resource/frontend/*" "AGENTS.md"
	@zip -u dist/${archive_file_name} release_info.json
	@rm release_info.json
	@echo "[SUCCESS] Archiving plugin to dist/ folder: Done!"

dev: check-remaining-flags-in-translations check-translations-consistency dist-clean
	@echo "[START] Archiving plugin to dist/ folder... (dev mode)"
	@cat plugin.json | json_pp > /dev/null
	@mkdir dist
	@zip -v -9 dist/${archive_file_name} -r .\
		--exclude "resource/frontend/*" \
		--exclude "docs/" \
		--exclude "scripts/" \
		--exclude "dev-*" \
		--exclude "dev*" \
		--exclude "resource/frontend/package-lock.json" \
        --exclude "tests/*" \
        --exclude "env/*" \
        --exclude ".git/*" \
        --exclude ".pytest_cache/*" \
        --exclude ".venv*" \
        --exclude ".env" \
        --exclude "node_modules/*" \
        --exclude "**/node_modules/*" \
        --exclude ".ruff_cache/*" \
        --exclude ".gitattributes" \
        --exclude ".gitignore" \
        --exclude ".gitattributes" \
		--exclude "resource/frontend/*"\
		--exclude "docs/*" \
		--exclude "dev-*"\
		--exclude "dev.*"\
		--exclude "scripts/*"\
		--exclude "AGENTS.md"\
        --exclude "*coverage*" \
        --exclude "**/.vscode/*" \
		--exclude "*test*" \
        --exclude "**/*.venv*" \
        --exclude "playwrite/*" \
        --exclude ".gitignore" \
        --exclude "playwright-report/*" \
        --exclude ".github/*" \
        --exclude "playwright.config.ts" \
        --exclude "python-lib/.mypy_cache/*" \
        --exclude "**/.DS_Store" \
        --exclude ".DS_Store" \
        --exclude ".wlock" \
        --exclude "**/.wlock" \
        --exclude "playwright/*" \
        --exclude "documentation/*" \
        --exclude "pyproject.toml"

	@echo "[SUCCESS] Archiving plugin to dist/ folder: Done!"


dist-clean:
	rm -rf dist



##########################################
######## Dev tools: backend #########
##########################################

backend-format:
	@echo "Running black ..."
	@$(PYTHON_BIN) -m black $(BACKEND_PYTHON_PATHS)

backend-format-check:
	@echo "Running black check ..."
	@$(PYTHON_BIN) -m black --check $(BACKEND_PYTHON_PATHS)

backend-lint-check:
	@echo "Running ruff ..."
	@$(PYTHON_BIN) -m ruff check $(BACKEND_PYTHON_PATHS)

backend-lint-fix:
	@echo "Running ruff --fix ..."
	@$(PYTHON_BIN) -m ruff check $(BACKEND_PYTHON_PATHS) --fix

backend-build-check:
	@echo "Running backend compile check ..."
	@$(PYTHON_BIN) -m compileall -q $(BACKEND_PYTHON_PATHS)

backend-mypy:
	@echo "Running mypy ..."
	@MYPYPATH=python-lib $(PYTHON_BIN) -m mypy -p backend --config-file=pyproject.toml

backend-unit-tests:
	@echo "Running unit tests ..."
	@PYTHONPATH=python-lib $(PYTHON_BIN) -m pytest -ra --durations=10 tests/unit

##########################################
######## Dev tools: frontend #########
##########################################

frontend-lint-check:
	@echo "Running lint ..."
	@cd resource/frontend && npm run lint .

frontend-format-check:
	@echo "Running Prettier check ..."
	@cd resource/frontend && npm run format:check

frontend-test-check:
	@echo "Running tests ..."
	@cd resource/frontend && npm run test

frontend-e2e-test:
	@echo "Running e2e tests ..."
	@cd resource/frontend && npm run test:e2e
	
# could be nice to rename as frontend-build ?
frontend-build-check:
	@echo "Running build check ..."
	@cd resource/frontend && npm run build:all


.PHONY: backend-format backend-format-check backend-lint-check backend-lint-fix backend-build-check backend-mypy backend-unit-tests frontend-lint-check frontend-format-check frontend-test-check frontend-build-check install-git-hooks dev-on dev-off sort-translations flag-translations unflag-translations pull-translations check-remaining-flags-in-translations check-translations-consistency
.SILENT: sort-translations flag-translations unflag-translations pull-translations check-remaining-flags-in-translations check-translations-consistency

install-git-hooks:
	@echo "Installing Git pre-commit hooks..."
	@hooks_path=$$(git config --get core.hooksPath || true); \
	if [ "$$hooks_path" = ".githooks" ]; then \
		echo "Migrating existing hooksPath from .githooks to pre-commit managed hooks..."; \
		git config --unset core.hooksPath; \
	fi
	@if ! command -v pre-commit >/dev/null 2>&1; then \
		echo "[ERROR] pre-commit is not installed. Run make setup or install dependencies first."; \
		exit 1; \
	fi
	@pre-commit install --hook-type pre-commit
	@echo "[SUCCESS] Git pre-commit hooks installed"

dev-on: install-git-hooks
	@if [ ! -f dev.patch ]; then echo "no dev.patch (create with: git diff > dev.patch)"; exit 1; fi
	@git apply dev.patch && echo "✅ dev on"

dev-off:
	@if [ ! -f dev.patch ]; then echo "no dev.patch"; exit 0; fi
	@if git apply -R --check dev.patch >/dev/null 2>&1; then \
		git apply -R dev.patch && echo "✅ dev off"; \
	else \
		echo "cannot reverse dev.patch; file changed"; exit 1; \
	fi
