.DEFAULT_GOAL := help

DOCKER_COMPOSE_UP=COMPOSE_PROJECT_NAME=internetrama-wordpress docker-compose up --no-recreate --remove-orphans -d

##
##
## 🧪 QA
##---------------------------------------------------------------------------
.PHONY: it code-coverage coding-standards coding-analysis tests
it: coding-standards coding-analysis tests  ## Runs the coding-standards, static-code-analysis, and tests targets

code-coverage: vendor ## Runs unit and integration tests with phpunit/phpunit with coverage
	vendor/bin/phpunit --colors=always --coverage-text

coding-standards: vendor  ## Normalizes composer.json and fixes code style issues
	vendor/bin/ecs check src lib tests --fix

coding-analysis: vendor  ## Runs a static code analysis with phpstan/phpstan
	vendor/bin/phpstan analyse -c phpstan.neon -l max src
	vendor/bin/phpstan analyse -c phpstan.neon -l 3 lib

tests: vendor ## Runs unit and integration tests with phpunit/phpunit
	vendor/bin/phpunit --colors=always

##
##
## 🚀 Utilities
##---------------------------------------------------------------------------
.PHONY: help

help: ## Show all make tasks (default)
	@grep -E '(^[a-zA-Z_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'

deploy: bundler ## Deploy application, ex: make deploy env=staging
	  @if test "$(env)" = "staging"; then \
		echo "This is executed in staging environment"; \
		(cd infra && bundle exec cap staging deploy); \
	  elif test "$(env)" = "prod"; then \
		echo "This is executed in production environment"; \
		(cd infra && bundle exec cap prod deploy); \
	  else \
		echo ""; \
		echo "Environment not found !"; \
	  fi

pull-data: bundler ## Pull database and media, ex: make pull-data env=staging
	  @if test "$(env)" = "staging"; then \
		echo "This is executed in staging environment"; \
		(cd infra && bundle exec cap staging app:db:pull); \
		(cd infra && bundle exec cap staging app:assets:pull_media); \
	  elif test "$(env)" = "prod"; then \
		echo "This is executed in production environment"; \
		(cd infra && bundle exec cap prod app:db:pull); \
		(cd infra && bundle exec cap staging app:assets:pull_media); \
	  else \
		echo ""; \
		echo "Environment not found !"; \
	  fi

vendor: composer.json composer.lock
	@composer validate --strict
	@composer install --no-interaction --no-progress
	@composer normalize

##
##
## 🔨 Project setup
##---------------------------------------------------------------------------
.PHONY: pre-configure start-docker start-local-srv stop deploy bundler start restart

pre-configure:
	@echo "Checking docker command"         && command -v "docker" > /dev/null 2>&1            || (echo "You have to install the "docker" command" && false)
	@echo "Checking docker-compose command" && command -v "docker-compose" > /dev/null 2>&1    || (echo "You have to install the "docker-compose" command" && false)

start-docker:
	$(DOCKER_COMPOSE_UP)

start-local-srv:
	COMPOSE_PROJECT_NAME=internetrama-wordpress HTTPS_PROXY=http://127.0.0.1:7080 symfony serve -d

bundler:
	@(cd infra && bundle install)

start: pre-configure start-docker start-local-srv ## Start project
restart: pre-configure stop start-docker start-local-srv

stop: ## Stop project
	COMPOSE_PROJECT_NAME=internetrama-wordpress docker-compose stop
	symfony server:stop
