.DEFAULT_GOAL := help

DOCKER_COMPOSE_FILE ?=compose.yml
DC=docker compose -f ${DOCKER_COMPOSE_FILE}
DC_RUN=${DC} run --rm
PHP=${DC_RUN} php
NPM=${DC_RUN} node npm
COMPOSER=${PHP} composer
ARTISAN=${PHP} php artisan
APP_ENV ?=dev

help:
	@printf "\033[33mUsage:\033[0m\n  make [target]\n\n\033[33mTargets:\033[0m\n"
	@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/'

.PHONY: help

##-----------------------------------------------------------------------------
##Docker

build: ## Build containers
	APP_ENV=${APP_ENV} $(DC) up -d --build

pull: ## Pull images
	$(DC) pull

down: ## Down containers
	$(DC) down --remove-orphans

.PHONY: build pull down

##-----------------------------------------------------------------------------
##Project

install: ## Installs and start the project
install: down pull build composer-install npm-install build-assets migrate load

up: ## Starts the project and skips composer & npm install & build assets
up: down pull build

composer-install: ## Composer install
	$(COMPOSER) install

composer-update: ## Composer update
	$(COMPOSER) update

cc: ## php artisan cache:clear --no-interaction --env=${APP_ENV}
	$(ARTISAN) cache:clear --no-interaction --env=${APP_ENV}

migration: ## php artisan make:migration --no-interaction
	$(ARTISAN) make:migration --no-interaction

migrate: ## php artisan migrate --no-interaction --env=${APP_ENV}
	$(ARTISAN) migrate --no-interaction --env=${APP_ENV}

load: ## php artisan db:seed --no-interaction --env=${APP_ENV}
	$(ARTISAN) db:seed --no-interaction --env=${APP_ENV}

npm-install: ## Npm install
	$(NPM) install

build-assets: ## Build assets
	$(NPM) run build

watch-assets: ## Watch for js / css changes
	$(NPM) run dev

.PHONY: install up composer-install composer-update cc migrate load npm-install build-assets

##-----------------------------------------------------------------------------
##Test

install-test: APP_ENV=test ## Installs the project with test environment
install-test: down pull build composer-install migrate load

test: ## Run all tests
	$(ARTISAN) test --coverage

test-feature: ## Run feature tests
	$(ARTISAN) test tests/Feature --coverage

test-unit: ## Run unit tests
	$(ARTISAN) test tests/Unit --coverage

.PHONY: install-test test test-feature test-unit
