.PHONY: help up down build logs shell artisan migrate seed tinker restart pull test test-unit test-feature test-coverage

# ── Configurações ─────────────────────────────────────────────────────────────
COMPOSE = docker compose
PHP     = $(COMPOSE) exec app
ARTISAN = $(PHP) php artisan

help: ## Mostra esta ajuda
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
	awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-18s\033[0m %s\n", $$1, $$2}'

# ── Ciclo de vida ─────────────────────────────────────────────────────────────
up: ## Sobe todos os serviços
	$(COMPOSE) up -d

down: ## Para todos os serviços
	$(COMPOSE) down

restart: ## Reinicia todos os serviços
	$(COMPOSE) restart

pull: ## Baixa imagens mais recentes e reinicia
	$(COMPOSE) pull
	$(COMPOSE) up -d --remove-orphans

build: ## Constrói a imagem localmente
	docker build -f docker/php/Dockerfile -t ticketpro:local .

logs: ## Acompanha os logs em tempo real
	$(COMPOSE) logs -f

logs-app: ## Logs apenas do container app
	$(COMPOSE) logs -f app

logs-queue: ## Logs do queue worker
	$(COMPOSE) logs -f queue

# ── Laravel ───────────────────────────────────────────────────────────────────
shell: ## Abre bash no container app
	$(PHP) bash

artisan: ## Executa artisan: make artisan CMD="migrate --force"
	$(ARTISAN) $(CMD)

migrate: ## Roda as migrações
	$(ARTISAN) migrate --force

migrate-fresh: ## Recria banco do zero (CUIDADO: apaga tudo!)
	$(ARTISAN) migrate:fresh --force

seed: ## Executa os seeders
	$(ARTISAN) db:seed --force

seed-global: ## Roda apenas o GlobalSettingsSeeder
	$(ARTISAN) db:seed --class=GlobalSettingsSeeder --force

tinker: ## Abre o Tinker
	$(ARTISAN) tinker

cache-clear: ## Limpa todos os caches
	$(ARTISAN) config:clear
	$(ARTISAN) route:clear
	$(ARTISAN) view:clear
	$(ARTISAN) cache:clear

cache-build: ## Recria todos os caches (produção)
	$(ARTISAN) config:cache
	$(ARTISAN) route:cache
	$(ARTISAN) view:cache

# ── Banco de dados ────────────────────────────────────────────────────────────
mysql: ## Abre o cliente MySQL
	$(COMPOSE) exec mysql mysql -u$$DB_USERNAME -p$$DB_PASSWORD $$DB_DATABASE

# ── Assets ────────────────────────────────────────────────────────────────────
build-assets: ## Compila os assets (JS/CSS) e limpa o modo dev
	$(PHP) npm run build
	rm -f public/hot

# ── Testes / TDD ─────────────────────────────────────────────────────────────
test: ## 🧪 Roda suite completa (Unit + Feature)
	$(ARTISAN) config:clear --ansi
	$(ARTISAN) test

test-unit: ## 🧪 Apenas testes Unit
	$(ARTISAN) config:clear --ansi
	$(ARTISAN) test --testsuite=Unit

test-feature: ## 🧪 Apenas testes Feature
	$(ARTISAN) config:clear --ansi
	$(ARTISAN) test --testsuite=Feature

test-coverage: ## 📊 Cobertura de código (requer Xdebug/PCOV)
	$(PHP) php -dpcov.enabled=1 artisan test --coverage-text

# ── Status ────────────────────────────────────────────────────────────────────
ps: ## Status de todos os containers
	$(COMPOSE) ps

health: ## Verifica saúde dos serviços
	@echo "=== App ===" && curl -sf http://localhost/up && echo " OK" || echo " FALHOU"
	@echo "=== MySQL ===" && $(COMPOSE) exec mysql mysqladmin ping -h localhost -u root --password=$$DB_ROOT_PASSWORD 2>/dev/null || echo " FALHOU"
	@echo "=== Redis ===" && $(COMPOSE) exec redis redis-cli ping || echo " FALHOU"
