.PHONY: install install-hooks update run format test build publish help

TEST_DATABASE ?= memory

.cache/tandem:
	@mkdir -p $$(dirname $@)
	@curl -fsSL https://raw.githubusercontent.com/rosszurowski/tandem/main/install.sh | bash -
	mv ./tandem .cache/tandem

install:.cache/tandem install-hooks ## installation des paquets et dépendances
	uv sync
	uv run jama migrate
	uv run jama loadfixtures
	@read -p "Do you want to create a django superuser? [y/N] " c;	echo; if test "$$c" = "Y" || test "$$c" = "y"; then uv run jama createsuperuser ; fi
	uv run jama newjamaproject default

install-hooks: ## installation des hooks git
	uv run pre-commit install --hook-type pre-commit --hook-type pre-push

build-ui-front: ## compilation du front de l'appli ui
	@if test -n "$$VUE3_JAMA_DIR"; then \
		echo "Building UI using VUE3_JAMA_DIR ($$VUE3_JAMA_DIR)"; \
		test -f "$$VUE3_JAMA_DIR/package.json" || { echo "VUE3_JAMA_DIR must point to the vue3-jama package directory" >&2; exit 1; }; \
		test -d "$$VUE3_JAMA_DIR/dist/assets" || { echo "VUE3_JAMA_DIR must contain a built dist directory" >&2; exit 1; }; \
		install_dir="$$(mktemp -d /tmp/jama-ui-front.XXXXXX)"; \
		trap 'rm -rf "$$install_dir"' EXIT; \
		cp src/ui/front/package.json src/ui/front/yarn.lock src/ui/front/.npmrc "$$install_dir/"; \
		PACKAGE_JSON="$$install_dir/package.json" node -e 'const fs = require("fs"); const path = require("path"); const packageJson = JSON.parse(fs.readFileSync(process.env.PACKAGE_JSON)); packageJson.dependencies["@pdn-certic/vue3-jama"] = "file:" + path.resolve(process.env.VUE3_JAMA_DIR); fs.writeFileSync(process.env.PACKAGE_JSON, JSON.stringify(packageJson, null, 2) + "\n");'; \
		yarn --cwd "$$install_dir" install --modules-folder "$(CURDIR)/src/ui/front/node_modules" --pure-lockfile; \
	else \
		echo "Building UI using vue3-jama from the published repository"; \
		yarn --cwd src/ui/front/ install; \
	fi
	yarn --cwd src/ui/front/ vite build
	@cp -r src/ui/front/node_modules/@pdn-certic/vue3-jama/dist/assets src/ui/front/dist/ui/

update: ## mise à jour des paquets et dépendances
	uv sync && uv run jama migrate

run: ## démarrage de l'environnement de développement
	@.cache/tandem 'uv run jama runserver' 'uv run jama db_worker' 'uv run jama crontask --no-heartbeat'

format: ## Formatage des sources avec Ruff
	uv run ruff check --fix
	uv run ruff format ./

test: ## Lancer les tests (TEST_DATABASE=memory|configured)
	@if test "$(TEST_DATABASE)" = "memory"; then \
		cd src && JAMA_VAR_DIR=.jama_test_data JAMA_SQLITE_DB_PATH=:memory: JAMA_USE_MODSHIB=0 uv run jama test --settings=jama.test_settings && rm -rf .jama_test_data; \
	elif test "$(TEST_DATABASE)" = "configured"; then \
		cd src && JAMA_USE_MODSHIB=0 uv run jama test --settings=jama.test_settings; \
	else \
		echo 'TEST_DATABASE must be either "memory" or "configured"' >&2; \
		exit 2; \
	fi

build:build-ui-front  ## build package
	rm -rf dist
	uv build

publish:build  ## publish package
	uv publish

help: ## Show this help
	@echo "\nChoisissez une commande. Les choix sont:\n"
	@grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[0;36m%-12s\033[m %s\n", $$1, $$2}'
	@echo ""

dependency-check:
	uv lock --upgrade
	osv-scanner scan --lockfile uv.lock
