.PHONY: help \
	build build-docker rebuild-docker \
	start start-local start-prod stop restart \
	test test-watch test-coverage \
	lint-check lint-fix \
	install-deps reinstall-deps clean \
	cache-clear cache-warm \
	logs shell routes serve-info

.ONESHELL:

ROOT_DIR        := $(CURDIR)
APP_DIR         := $(ROOT_DIR)/app
DOCKER_CMD      := docker compose -f docker-compose.yml
DOCKER_DEV_CMD  := docker compose -f docker-compose.yml -f docker-compose.dev.yml

EXEC            := $(DOCKER_DEV_CMD) run --rm web

BLUE   := \033[0;34m
GREEN  := \033[0;32m
YELLOW := \033[0;33m
NC     := \033[0m

.DEFAULT_GOAL := help

help: ## Show available targets
	@echo "$(BLUE)╔════════════════════════════════════════════════════════════════════╗$(NC)"
	@echo "$(BLUE)║       Boathouse Cafe - Build, Test, Start & Debug Helper           ║$(NC)"
	@echo "$(BLUE)╚════════════════════════════════════════════════════════════════════╝$(NC)"
	@echo ""
	@_targets=$$(grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST)); \
	_print() { echo "$$_targets" | grep -E "$$1" | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(YELLOW)%-20s$(NC) %s\n", $$1, $$2}'; }; \
	echo "$(GREEN)Build$(NC)";        _print '^build|^rebuild'; \
	echo ""; \
	echo "$(GREEN)Run$(NC)";          _print '^start|^stop|^restart'; \
	echo ""; \
	echo "$(GREEN)Test$(NC)";         _print '^test'; \
	echo ""; \
	echo "$(GREEN)Lint$(NC)";         _print '^lint'; \
	echo ""; \
	echo "$(GREEN)Dependencies$(NC)"; _print '^install|^reinstall|^clean'; \
	echo ""; \
	echo "$(GREEN)Database$(NC)";     _print '^db'; \
	echo ""; \
	echo "$(GREEN)Utilities$(NC)";    _print '^cache|^routes|^serve|^logs|^shell'

# -- Build --------------------------------------------------------------------

build: ## Install deps and clear cache
	@$(EXEC) composer install
	@$(EXEC) php bin/console cache:clear

build-docker: ## Build Docker image
	@$(DOCKER_CMD) build

rebuild-docker: ## Rebuild Docker image from scratch and start
	@$(DOCKER_CMD) down
	@$(DOCKER_CMD) build --no-cache
	@$(DOCKER_CMD) up -d

# -- Run ----------------------------------------------------------------------

start: ## Start Docker containers in dev mode (live reload)
	@$(DOCKER_DEV_CMD) up -d
	@echo "$(GREEN)Running at http://localhost:8087$(NC)"

start-local: ## Start Symfony dev server locally
	@$(EXEC) symfony serve -d
	@echo "$(GREEN)Running at http://localhost:8087$(NC)"

start-prod: ## Start in production mode
	@$(EXEC) sh -c "APP_ENV=prod php bin/console cache:clear && symfony serve"

stop: ## Stop all services (Docker + local)
	@$(DOCKER_DEV_CMD) down

restart: stop start ## Restart Docker containers

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

test: ## Run tests
	@$(EXEC) php bin/phpunit

test-watch: ## Run tests in watch mode
	@$(EXEC) php bin/phpunit --testdox --watch

test-coverage: ## Generate HTML coverage report in app/coverage/
	@$(EXEC) php bin/phpunit --coverage-html coverage

# -- Lint ---------------------------------------------------------------------

lint-check: ## Check code style without fixing
	@$(EXEC) vendor/bin/php-cs-fixer fix --diff --dry-run

lint-fix: ## Fix code style
	@$(EXEC) vendor/bin/php-cs-fixer fix

# -- Dependencies & Cleanup ---------------------------------------------------

install-deps: ## Install Composer dependencies
	@$(EXEC) composer install

reinstall-deps: ## Remove vendor and reinstall dependencies
	@$(EXEC) sh -c "rm -rf vendor composer.lock && composer install"

clean: ## Remove cache and logs
	@$(EXEC) sh -c "rm -rf var/cache/* var/log/*"

# -- Utilities ----------------------------------------------------------------

cache-clear: ## Clear application cache
	@$(EXEC) php bin/console cache:clear

cache-warm: ## Warm up application cache
	@$(EXEC) php bin/console cache:warmup

routes: ## List all routes
	@$(EXEC) php bin/console debug:router

serve-info: ## Show Symfony server status
	@$(EXEC) symfony server:status || echo "Server not running"

logs: ## Follow Docker container logs
	@$(DOCKER_DEV_CMD) logs -f

shell: ## Open shell in Docker container
	@$(DOCKER_DEV_CMD) run --rm -it web sh
