# ─── Heimdall — Development & Deployment ──────────────────────────
.DEFAULT_GOAL := help
.PHONY: help build test docker-up docker-down docker-build clean dev

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

build: ## Build the .NET solution
	dotnet build

test: ## Run all tests
	dotnet test

test-watch: ## Run tests in watch mode
	dotnet watch test --project tests/Heimdall.Domain.Tests

clean: ## Clean build artifacts
	dotnet clean && rm -rf src/**/bin src/**/obj tests/**/bin tests/**/obj

# ─── Docker ──────────────────────────────────────────────────────

docker-up: ## Start all services (dev)
	docker compose -f docker/docker-compose.yml -f docker/docker-compose.override.yml up -d

docker-down: ## Stop all services
	docker compose -f docker/docker-compose.yml down -v

docker-build: ## Rebuild images without cache
	docker compose -f docker/docker-compose.yml build --no-cache

docker-logs: ## Tail logs
	docker compose -f docker/docker-compose.yml logs -f

docker-ps: ## List running services
	docker compose -f docker/docker-compose.yml ps

docker-reset: ## Stop and remove everything
	docker compose -f docker/docker-compose.yml down -v --rmi all

# ─── Deployment ──────────────────────────────────────────────────

deploy: docker-build docker-up ## Build and deploy

# ─── Database ────────────────────────────────────────────────────

migration-add: ## Add a new EF migration (usage: make migration-add NAME=xxx)
	dotnet ef migrations add $(NAME) -p src/Heimdall.Infrastructure

migration-remove: ## Remove last migration
	dotnet ef migrations remove -p src/Heimdall.Infrastructure

# ─── Dev helpers ─────────────────────────────────────────────────

dev-api: ## Run combined API+Web in development mode (API and Blazor UI in one process)
	DATABASE_URL="Host=localhost;Port=5432;Database=heimdall;Username=heimdall;Password=heimdall" \
	JWT__Key="ThisIsADevKeyThatIsAtLeast32BytesLong!!" \
	ASPNETCORE_ENVIRONMENT=Development \
	Cors__AllowedOrigins="*" \
	dotnet run --project src/Heimdall.Web

dev-web: ## Run combined API+Web+Jobs in development mode (single process)
	DATABASE_URL="Host=localhost;Port=5432;Database=heimdall;Username=heimdall;Password=heimdall" \
	JWT__Key="ThisIsADevKeyThatIsAtLeast32BytesLong!!" \
	ASPNETCORE_ENVIRONMENT=Development \
	Cors__AllowedOrigins="*" \
	dotnet run --project src/Heimdall.Web
