# ==============================================================================
# EZSCALE — Makefile shortcuts for the docker compose dev stack
# ==============================================================================

SHELL := /bin/bash
DC    := docker compose
EXEC  := $(DC) exec -T app
RUN   := $(DC) run --rm app

.DEFAULT_GOAL := help

# -------------------------------------------------------------------- lifecycle
.PHONY: help
help: ## Show this help
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf "  \033[36m%-20s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)

.PHONY: init
init: ## First-time setup: copy .env.docker, build, install deps, migrate
	@if [ ! -f .env.docker ]; then cp .env.docker.example .env.docker && echo "[init] created .env.docker"; fi
	$(DC) build --pull
	$(DC) up -d mariadb valkey
	@echo "[init] waiting 10s for mariadb to settle..."
	@sleep 10
	$(RUN) composer install
	$(DC) run --rm vite npm install
	$(DC) up -d
	@echo ""
	@echo "Stack is up. Open https://ezscale.docker.localhost"

.PHONY: up
up: ## Start all services in the background
	$(DC) up -d

.PHONY: down
down: ## Stop all services (volumes preserved)
	$(DC) down

.PHONY: restart
restart: ## Restart all services
	$(DC) restart

.PHONY: build
build: ## Rebuild all images
	$(DC) build --pull

.PHONY: rebuild
rebuild: ## Rebuild from scratch (no cache)
	$(DC) build --pull --no-cache

.PHONY: destroy
destroy: ## Stop everything AND wipe data volumes (DB, mail, etc.)
	$(DC) down -v

.PHONY: ps
ps: ## Show service status
	$(DC) ps

# ------------------------------------------------------------------------ logs
.PHONY: logs
logs: ## Tail logs (use SVC=name to scope to one service)
ifdef SVC
	$(DC) logs -f $(SVC)
else
	$(DC) logs -f
endif

# -------------------------------------------------------------------- shells
.PHONY: sh
sh: ## Bash inside the app container
	$(DC) exec app bash

.PHONY: web-sh
web-sh: ## Shell inside the nginx container
	$(DC) exec web sh

.PHONY: db-sh
db-sh: ## MariaDB CLI
	$(DC) exec mariadb mariadb -uezscale -pezscale_local ezscale_billing

.PHONY: redis-sh
redis-sh: ## Valkey CLI
	$(DC) exec valkey valkey-cli

# ----------------------------------------------------------- artisan / composer
.PHONY: artisan
artisan: ## Run an artisan command (use ARGS="migrate")
	$(EXEC) php artisan $(ARGS)

.PHONY: composer
composer: ## Run composer (use ARGS="require ...")
	$(EXEC) composer $(ARGS)

.PHONY: tinker
tinker: ## Open a tinker shell
	$(DC) exec app php artisan tinker

# ---------------------------------------------------------------------- frontend
.PHONY: npm
npm: ## Run npm in the vite container (use ARGS="install ...")
	$(DC) exec vite npm $(ARGS)

.PHONY: vite-build
vite-build: ## One-off production build
	$(DC) run --rm vite npm run build

# ----------------------------------------------------------------------- testing
.PHONY: test
test: ## Run the Pest suite
	$(EXEC) php artisan test --compact $(ARGS)

.PHONY: pint
pint: ## Format dirty PHP files
	$(EXEC) vendor/bin/pint --dirty --format agent

# -------------------------------------------------------------- DB convenience
.PHONY: migrate
migrate: ## Run pending migrations
	$(EXEC) php artisan migrate

.PHONY: fresh
fresh: ## Drop all tables, re-migrate, seed
	$(EXEC) php artisan migrate:fresh --seed

.PHONY: seed
seed: ## Run seeders
	$(EXEC) php artisan db:seed

# --------------------------------------------------------------- diagnostics
.PHONY: doctor
doctor: ## Print key compose info for debugging
	$(DC) config --services
	@echo ""
	$(DC) ps
	@echo ""
	$(DC) exec app php -v 2>/dev/null || echo "app not running"
