.DEFAULT_GOAL := help

## —— Help ————————————————————————————————————————
help: ## Affiche la liste des commandes disponibles
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

## —— Docker ——————————————————————————————————————
build_dev: ## Build les images Docker pour le dev
	docker compose -f docker-compose-dev.yml build

build_prod: ## Build les images Docker pour la prod
	docker compose -f docker-compose-prod.yml build

start_dev: ## Lance les containers dev
	docker compose -f docker-compose-dev.yml up

start_prod: ## Lance les containers prod en background
	docker compose -f docker-compose-prod.yml up -d

stop_dev: ## Arrete les containers dev
	docker compose -f docker-compose-dev.yml down

purge_dev: ## Arrete et purge les containers dev (volumes inclus)
	docker compose -f docker-compose-dev.yml down -v

stop_prod: ## Arrete les containers prod
	docker compose -f docker-compose-prod.yml down

## —— Git ——————————————————————————————————————————
pull_dev: ## Pull les images Docker dev
	docker compose -f docker-compose-dev.yml pull

pull_prod: ## Pull les images Docker prod
	docker compose -f docker-compose-prod.yml pull

## —— Ansible —————————————————————————————————————
deploy_prod: ## Deploy en prod via Ansible (demande le vault password)
	 ansible-playbook ansible/deploy.yml -i ansible/hosts.ini --ask-vault-pass

## —— Install ——————————————————————————————————————
install_dev: ## Install les dependances dev via Docker
	docker compose -f docker-compose-dev.yml exec php composer install
	docker compose -f docker-compose-dev.yml exec bun bun install

install_prod: ## Install les dependances et build les assets pour la prod
	docker compose -f docker-compose-prod.yml exec php composer install --no-dev --optimize-autoloader
	bun install
	bun run build

## —— Tests ————————————————————————————————————————
test_db_create: ## Cree la base de donnees de test
	docker compose -f docker-compose-dev.yml exec database psql -U app -d e_ticket -tc "SELECT 1 FROM pg_database WHERE datname = 'e_ticket_test'" | grep -q 1 || \
		docker compose -f docker-compose-dev.yml exec database psql -U app -d e_ticket -c "CREATE DATABASE e_ticket_test"

test_db_schema: ## Cree/met a jour le schema de la base de test
	docker compose -f docker-compose-dev.yml exec php php bin/console doctrine:schema:update --force --env=test

test_db_migrate: ## Execute les migrations sur la base de test
	docker compose -f docker-compose-dev.yml exec php php bin/console doctrine:migrations:migrate --no-interaction --env=test

test_db_reset: ## Supprime et recree la base de test depuis zero
	docker compose -f docker-compose-dev.yml exec database psql -U app -d e_ticket -c "DROP DATABASE IF EXISTS e_ticket_test"
	docker compose -f docker-compose-dev.yml exec database psql -U app -d e_ticket -c "CREATE DATABASE e_ticket_test"
	$(MAKE) test_db_schema

test_db_setup: ## Setup complet de la base de test (cree si besoin + schema)
	$(MAKE) test_db_create
	$(MAKE) test_db_schema

run_test: ## Lance les tests PHP et JS via Docker dev
	docker compose -f docker-compose-dev.yml exec php php bin/phpunit
	docker compose -f docker-compose-dev.yml exec bun bun run test

run_test_php: ## Lance uniquement les tests PHP via Docker dev
	docker compose -f docker-compose-dev.yml exec php php bin/phpunit

run_test_js: ## Lance uniquement les tests JS via Docker dev
	docker compose -f docker-compose-dev.yml exec bun bun run test

run_test_coverage_php: ## Lance les tests PHP avec couverture via Docker dev
	docker compose -f docker-compose-dev.yml exec php php bin/phpunit --coverage-text --coverage-html=coverage/php

run_test_coverage_js: ## Lance les tests JS avec couverture via Docker dev
	docker compose -f docker-compose-dev.yml exec bun bun run test -- --coverage

run_test_file: ## Lance un fichier de test PHP specifique (usage: make run_test_file FILE=tests/Service/AuditServiceTest.php)
	docker compose -f docker-compose-dev.yml exec php php bin/phpunit $(FILE)

## —— PWA —————————————————————————————————————————
pwa_dev: ## Compile les assets PWA en dev via Docker
	docker compose -f docker-compose-dev.yml exec php php bin/console pwa:compile

pwa_prod: ## Compile les assets PWA en prod via Docker
	docker compose -f docker-compose-prod.yml exec php php bin/console pwa:compile --env=prod

## —— Traductions ——————————————————————————————————
trans: ## Traduit les fichiers YAML FR vers EN/ES/DE/IT via LibreTranslate (Docker dev)
	docker compose -f docker-compose-dev.yml exec php php bin/console app:translate
	docker compose -f docker-compose-dev.yml stop libretranslate

trans_prod: ## Traduit les fichiers YAML FR vers EN/ES/DE/IT via LibreTranslate (Docker prod)
	docker compose -f docker-compose-prod.yml exec -T php php bin/console app:translate --env=prod
	docker compose -f docker-compose-prod.yml stop libretranslate

## —— Qualite ——————————————————————————————————————
hadolint: ## Lance Hadolint sur les Dockerfiles
	hadolint docker/php/dev/Dockerfile
	hadolint docker/php/prod/Dockerfile

## —— Symfony ——————————————————————————————————————
entity: ## Creer ou modifier une entite via Docker dev
	docker compose -f docker-compose-dev.yml exec php php bin/console make:entity

## —— Database ——————————————————————————————————————
migration_dev: ## Genere une migration via Docker dev
	docker compose -f docker-compose-dev.yml exec php php bin/console make:migration

migrate_dev: ## Execute les migrations via Docker dev
	docker compose -f docker-compose-dev.yml exec php php bin/console doctrine:migrations:migrate --no-interaction

force_sql_dev: ## Force la mise a jour du schema de la base de donnees dev
	docker compose -f docker-compose-dev.yml exec php php bin/console doctrine:schema:update --force

migrate_prod: ## Execute les migrations en prod via Docker
	docker compose -f docker-compose-prod.yml exec php php bin/console doctrine:migrations:migrate --no-interaction --env=prod

## —— Cache ————————————————————————————————————————
clear_prod: ## Clear le cache Symfony et le pool opcache en prod via Docker
	docker compose -f docker-compose-prod.yml exec php php bin/console cache:clear --env=prod
	docker compose -f docker-compose-prod.yml exec php php bin/console cache:pool:clear --all --env=prod

purge_liip_dev: ## Purge le cache Liip Imagine en dev via Docker
	docker compose -f docker-compose-dev.yml exec php php bin/console liip:imagine:cache:remove

purge_liip_prod: ## Purge le cache Liip Imagine en prod via Docker
	docker compose -f docker-compose-prod.yml exec php php bin/console liip:imagine:cache:remove --env=prod

## —— Maintenance ——————————————————————————————————
maintenance_on: ## Active le mode maintenance
	touch public/.update

maintenance_off: ## Desactive le mode maintenance
	rm -f public/.update
