# Permet d'utiliser un .env.docker.local pour override
ENV_DEFAULT = docker/.env.docker
ENV_LOCAL   = docker/.env.docker.local
ENV_FILE    := $(if $(wildcard $(ENV_LOCAL)),$(ENV_LOCAL),$(ENV_DEFAULT))

# Permet d'avoir les variables du fichier .env.docker.local
include $(ENV_DEFAULT)
-include $(ENV_LOCAL)

PHP_CONTAINER				= php-$(DOCKER_APP_NAME)-apache
SYMFONY_CONSOLE				= $(EXEC_PHP) php bin/console

DOCKER_COMPOSE = docker compose --env-file $(ENV_FILE)
DOCKER						= docker

EXEC_PHP					= $(DOCKER) exec -t -u $(APP_USER) $(PHP_CONTAINER)
EXEC_PHP_CS_FIXER			= $(EXEC_PHP) php vendor/bin/php-cs-fixer
EXEC_PHP_ROOT				= $(DOCKER) exec -t -u root $(PHP_CONTAINER)
EXEC_PHP_INTERACTIVE		= $(DOCKER) exec -it -u $(APP_USER) $(PHP_CONTAINER)
EXEC_PHP_INTERACTIVE_ROOT	= $(DOCKER) exec -it -u root $(PHP_CONTAINER)
FILES						=
DATA_SQL					?= data.sql
DATA_SQL_NORM				?= data_norm.sql

#========================================================================================

env-init:
	@mkdir -p docker
	@cp --update=none $(ENV_DEFAULT) $(ENV_LOCAL)

# Lance le container
start: env-init
	@echo "**** START CONTAINERS ****"
	@cp --update=none docker/.env.docker docker/.env.docker.local
	CURRENT_UID=$(shell id -u) CURRENT_GID=$(shell id -g) $(DOCKER_COMPOSE) up -d
	@echo ""
	@echo "URLs disponibles:"
	@echo "- Symfony API: http://localhost:8081/api"
	@echo "- Nuxt (Inventory_frontend): http://localhost:3001"
	@echo "- adminer: http://localhost:5050"

# Éteint le container
stop:
	$(DOCKER_COMPOSE) stop

restart: env-init
	$(DOCKER_COMPOSE) down
	CURRENT_UID=$(shell id -u) CURRENT_GID=$(shell id -g) $(DOCKER_COMPOSE) up -d

install: copy-git-hook composer-install cache-clear node-use build-nuxtJS

# Supprime tout est réinstalle tout (Attention ça supprime la bdd aussi)
reset: delete_built_dir remove_orphans build-without-cache start wait install

composer-install:
	$(EXEC_PHP) composer install

build-nuxtJS:
#	$(EXEC_PHP) cp -n Inventory_frontend/.env.dist Inventory_frontend/.env.local
	$(EXEC_PHP) sh -lc "cd Inventory_frontend && npm install && npm run generate"

dev-nuxt:
	$(EXEC_PHP) sh -lc "cd Inventory_frontend && npm run dev"

delete_built_dir:
	CURRENT_UID=$(shell id -u) CURRENT_GID=$(shell id -g) $(DOCKER_COMPOSE) up -d
	$(DOCKER) exec -u root $(PHP_CONTAINER) rm -rf vendor/
	$(DOCKER) exec -u root $(PHP_CONTAINER) rm -rf Inventory_frontend/node_modules

remove_orphans:
	$(DOCKER_COMPOSE) kill
	$(DOCKER_COMPOSE) down --volumes --remove-orphans

build-without-cache:
	$(DOCKER_COMPOSE) build	\
		--build-arg="DOCKER_PHP_VERSION=$(DOCKER_PHP_VERSION)" 	\
		--build-arg="DOCKER_NODE_VERSION=$(DOCKER_NODE_VERSION)" 	\
		--build-arg="CURRENT_UID=$(shell id -u)" 	\
		--build-arg="CURRENT_GID=$(shell id -g)" 	\
 		--no-cache

# Attention, supprime votre bdd local
db-reset:
	$(DOCKER_COMPOSE) down -v
	$(DOCKER_COMPOSE) up -d

# Restart la bdd
db-restart:
	$(DOCKER_COMPOSE) down
	$(DOCKER_COMPOSE) up -d

cache-clear:
	$(SYMFONY_CONSOLE) cache:clear

cache-clear-full:
	$(SYMFONY_CONSOLE) cache:clear
	$(EXEC_PHP) rm -rf var/cache/*

copy-git-hook:
	$(EXEC_PHP) cp pre-commit .git/hooks/
	$(EXEC_PHP) cp commit-msg .git/hooks/
	$(EXEC_PHP) chmod a+x .git/hooks/pre-commit
	$(EXEC_PHP) chmod a+x .git/hooks/commit-msg

shell:
	$(EXEC_PHP_INTERACTIVE) bash

# Force la version node
node-use:
	bash -lc 'source "$$HOME/.nvm/nvm.sh" && nvm install && nvm use'

# Utilisé par le pre-commit pour fix les fichiers modifiés
php-cs-fixer-allow-risky:
	@echo "Fixing files: $(FILES)"
	$(EXEC_PHP_CS_FIXER) fix --config=.php-cs-fixer.dist.php --allow-risky=yes $(FILES)

test:
	$(EXEC_PHP) php -d memory_limit="512M" vendor/bin/phpunit $(FILES)

test-setup:
	$(SYMFONY_CONSOLE) doctrine:database:create --if-not-exists --env=test
	$(SYMFONY_CONSOLE) doctrine:schema:update --force --env=test

wait:
	sleep 10

# Normalize pgAdmin data-only dump and import into DB
import-data:
	python3 scripts/normalize-dump.py $(DATA_SQL) $(DATA_SQL_NORM) --lower
	$(DOCKER_COMPOSE) exec -T db psql -U $(POSTGRES_USER) -d $(POSTGRES_DB) -v ON_ERROR_STOP=1 -c "SET session_replication_role = replica;"
	$(DOCKER_COMPOSE) exec -T db psql -U $(POSTGRES_USER) -d $(POSTGRES_DB) -v ON_ERROR_STOP=1 < $(DATA_SQL_NORM)
	$(DOCKER_COMPOSE) exec -T db psql -U $(POSTGRES_USER) -d $(POSTGRES_DB) -v ON_ERROR_STOP=1 -c "SET session_replication_role = DEFAULT;"

# Fixtures management
fixtures-dump:
	@echo "Dumping current database to fixtures/data.sql..."
	$(DOCKER_COMPOSE) exec -T db pg_dump -U $(POSTGRES_USER) -d $(POSTGRES_DB) \
		--data-only --inserts --no-owner --no-privileges \
		--exclude-table=doctrine_migration_versions \
		| grep -v "^pg_dump:" | grep -v "^\\\\restrict" > fixtures/data.sql
	@echo "Fixtures saved to fixtures/data.sql"

fixtures-load:
	@echo "Loading fixtures from fixtures/data.sql (FK checks disabled)..."
	$(DOCKER_COMPOSE) exec -T db psql -U $(POSTGRES_USER) -d $(POSTGRES_DB) -c "SET session_replication_role = replica;"
	-$(DOCKER_COMPOSE) exec -T db psql -U $(POSTGRES_USER) -d $(POSTGRES_DB) < fixtures/data.sql
	$(DOCKER_COMPOSE) exec -T db psql -U $(POSTGRES_USER) -d $(POSTGRES_DB) -c "SET session_replication_role = DEFAULT;"
	@echo "Fixtures loaded!"

fixtures-reset:
	@echo "Resetting database and loading fixtures..."
	$(DOCKER_COMPOSE) exec -T db psql -U $(POSTGRES_USER) -d $(POSTGRES_DB) -c "DO \$$\$$ DECLARE r RECORD; BEGIN FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public' AND tablename != 'doctrine_migration_versions') LOOP EXECUTE 'TRUNCATE TABLE ' || quote_ident(r.tablename) || ' CASCADE'; END LOOP; END \$$\$$;"
	$(MAKE) fixtures-load
