# Executables (local)
DOCKER_COMP = docker compose

DOCKER_COMP_EXEC:=$(DOCKER_COMP) exec

# Docker containers
PHP_CONT = $(DOCKER_COMP_EXEC) php
NODE_CONT = $(DOCKER_COMP_EXEC) node



# Executables
PHP      = $(PHP_CONT) php
COMPOSER = $(PHP_CONT) composer
SYMFONY  = $(PHP) bin/console
SF_MEMORY = $(PHP) -d memory_limit=256M bin/console

# Misc
.DEFAULT_GOAL = help
.PHONY        : help build start install down stop logs sh composer vendor sf cc test phpmd phpcs quality fix-permissions controller entity migration migration-diff migration-migrate form crud fixtures command auth subscriber state-processor state-provider npm-install npm-run npm-watch openapi

## —— 🎵 🐳 The Symfony Docker Makefile 🐳 🎵 ——————————————————————————————————
help: ## Outputs this help screen
	@grep -E '(^[a-zA-Z0-9\./_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}{printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'

## —— Docker 🐳 ————————————————————————————————————————————————————————————————
build: ## Builds the Docker images
	@$(DOCKER_COMP) build --pull --no-cache

phparkitect: ## Vérifie l'architecture avec PHPArkitect
	@$(PHP_CONT) vendor/bin/phparkitect check

up: ## Start the docker hub
	@$(DOCKER_COMP) up -d

start: ## Start the docker hub in detached mode (no logs)
	@$(DOCKER_COMP) up --pull always -d --wait

install: build start vendor npm-install## Build and start the containers

down: ## Stop and remove the docker hub
	@$(DOCKER_COMP) down --remove-orphans

stop: ## Stop the docker hub
	@$(DOCKER_COMP) stop

logs: ## Show live logs
	@$(DOCKER_COMP) logs --tail=0 --follow

sh: ## Connect to the FrankenPHP container
	@$(PHP_CONT) sh

test: ## Start tests with phpunit, pass the parameter "c=" to add options to phpunit, example: make test c="--group e2e --stop-on-failure", or "f=" to specify a test file, example: make test f="ScrapeChapterHandlerTest"
	@$(eval c ?=)
	@$(eval f ?=)
	@$(DOCKER_COMP) exec -e APP_ENV=test php bin/phpunit $(c) $(if $(f),--filter=$(f),)

phpmd: ## Start PHP Mess Detector
	@if ! $(DOCKER_COMP) exec php vendor/bin/phpmd src/ text phpmd.xml -v; then \
    		echo "Done"; \
	fi

phpcs: ## Start PHP Code Sniffer
	@$(DOCKER_COMP) exec php vendor/bin/php-cs-fixer fix src/ --rules=@PSR12

quality: phpmd phpcs ## Start PHP Mess Detector and PHP Code Sniffer

fix-permissions: ## Fix permissions
	@$(DOCKER_COMP) run --rm --user="root" php chown -R $$(id -u):$$(id -g) .

tail-logs: ## Tail the logs
	@$(DOCKER_COMP) logs -f

## —— Composer 🧙 ——————————————————————————————————————————————————————————————
composer: ## Run composer, pass the parameter "c=" to run a given command, example: make composer c='req symfony/orm-pack'
	@$(eval c ?=)
	@$(COMPOSER) $(c)

vendor: ## Install vendors according to the current composer.lock file
vendor: c=install --prefer-dist --no-progress --no-scripts --no-interaction
vendor: composer

## —— Symfony 🎵 ———————————————————————————————————————————————————————————————
sf: ## List all Symfony commands or pass the parameter "c=" to run a given command, example: make sf c=about
	@$(eval c ?=)
	@$(SYMFONY) $(c)

cc: c=c:c ## Clear the cache
cc: sf

controller: ## Create a new controller
	@$(SYMFONY) make:controller

entity: ## Create a new entity
	@$(SYMFONY) make:entity

migration: ## Create a new migration
	@$(SYMFONY) make:migration

migration-diff: ## Create a new migration diff
	@$(SYMFONY) make:migration --no-interaction --allow-empty-diff

migration-migrate: ## Migrate the database
	@$(SYMFONY) doctrine:migrations:migrate --no-interaction

migration-revert: ## Revert the last migration
	@$(SYMFONY) doctrine:migrations:migrate prev --no-interaction

form: ## Create a new form
	@$(SYMFONY) make:form

crud: ## Create CRUD for an existing entity
	@$(SYMFONY) make:crud

factory: ## Create a new factory
	@$(SYMFONY) make:factory

fixtures: ## Make fixtures
	@$(SYMFONY) make:fixtures

fixtures-load: ## Load fixtures
	@$(SF_MEMORY) doctrine:fixtures:load --no-interaction

command: ## Create a new command
	@$(SYMFONY) make:command

auth: ## Create a new authenticator
	@$(SYMFONY) make:auth

subscriber: ## Create a new event subscriber
	@$(SYMFONY) make:subscriber

state-processor: ## Create a new state processor
	@$(SYMFONY) make:state-processor

state-provider: ## Create a new state provider
	@$(SYMFONY) make:state-provider

twig-component: ## Create a new twig component
	@$(SYMFONY) make:twig-component

twig-extension: ## Create a new twig extension
	@$(SYMFONY) make:twig-extension

stimulus: ## Create a new stimulus controller
	@$(SYMFONY) make:stimulus-controller

consume-commands: ## Consume commands messages
	@$(SYMFONY) messenger:consume commands -vv

consume-events: ## Consume events messages
	@$(SYMFONY) messenger:consume events -vv

consume-schedule: ## Consume schedule messages
	@$(SYMFONY) messenger:consume async -vv scheduler_default

message: ## Create a new message and handler
	@$(SYMFONY) make:message

openapi: ## Exporter la documentation OpenAPI en JSON
	@$(SYMFONY) api:openapi:export --output=public/api-docs.json

## —— Webpack Encore —————————————————————————————————————————————————————————————
npm-install: ## Install npm dependencies
	@$(DOCKER_COMP) exec node npm install --force

npm-run: ## Run the dev server
	@$(DOCKER_COMP) exec node npm run dev

npm-watch: ## Watch for changes
	@$(DOCKER_COMP) exec node npm run watch

npm-add: ## Add a package as a dependency make npm-add p=package-name
	@$(DOCKER_COMP) exec node npm install $(p)

npm-add-dev: ## Add a package as a dev dependency make npm-add-dev p=package-name
	@$(DOCKER_COMP) exec node npm install $(p) --save-dev

npm-remove: ## Remove a package make npm-remove p=package-name
	@$(DOCKER_COMP) exec node npm uninstall $(p)
