.PHONY: help install build test test-unit test-integration test-e2e test-cov clean docker-test-all

# Color output
BLUE := \033[0;34m
GREEN := \033[0;32m
YELLOW := \033[0;33m
RED := \033[0;31m
NC := \033[0m # No Color

help: ## Show this help message
	@echo '$(BLUE)Admin Service - Available Commands:$(NC)'
	@echo ''
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(GREEN)%-20s$(NC) %s\n", $$1, $$2}'
	@echo ''

install: ## Install dependencies
	@echo '$(BLUE)Installing dependencies...$(NC)'
	npm install

build: ## Build the application
	@echo '$(BLUE)Building application...$(NC)'
	npm run build

prisma-generate: ## Generate Prisma client
	@echo '$(BLUE)Generating Prisma client...$(NC)'
	npm run prisma:generate

prisma-migrate: ## Run Prisma migrations
	@echo '$(BLUE)Running Prisma migrations...$(NC)'
	npm run prisma:migrate

test: ## Run all tests
	@echo '$(BLUE)Running all tests...$(NC)'
	npm test

test-unit: ## Run unit tests only
	@echo '$(BLUE)Running unit tests...$(NC)'
	npm run test:unit

test-integration: ## Run integration tests only
	@echo '$(BLUE)Running integration tests...$(NC)'
	@echo '$(YELLOW)Note: Requires test database to be running$(NC)'
	npm run test:integration

test-e2e: ## Run end-to-end tests only
	@echo '$(BLUE)Running E2E tests...$(NC)'
	@echo '$(YELLOW)Note: Requires test database to be running$(NC)'
	npm run test:e2e

test-cov: ## Run tests with coverage
	@echo '$(BLUE)Running tests with coverage...$(NC)'
	npm run test:cov
	@echo '$(GREEN)Coverage report generated in ./coverage$(NC)'

test-watch: ## Run tests in watch mode
	@echo '$(BLUE)Running tests in watch mode...$(NC)'
	npm run test:watch

clean: ## Clean build artifacts and dependencies
	@echo '$(BLUE)Cleaning build artifacts...$(NC)'
	rm -rf dist coverage node_modules
	@echo '$(GREEN)Clean complete$(NC)'

docker-test-all: ## Run all tests in Docker container
	@echo '$(BLUE)Running tests in Docker...$(NC)'
	@echo '$(YELLOW)Building test container...$(NC)'
	docker build -f Dockerfile.test -t admin-service-test .
	@echo '$(YELLOW)Running tests...$(NC)'
	docker run --rm \
		-e DATABASE_URL="postgresql://postgres:password@host.docker.internal:5432/admin_service_test?schema=public" \
		admin-service-test
	@echo '$(GREEN)Docker tests complete$(NC)'

lint: ## Run linter
	@echo '$(BLUE)Running linter...$(NC)'
	npm run lint

format: ## Format code
	@echo '$(BLUE)Formatting code...$(NC)'
	npm run format

dev: ## Start development server
	@echo '$(BLUE)Starting development server...$(NC)'
	npm run start:dev

start: ## Start production server
	@echo '$(BLUE)Starting production server...$(NC)'
	npm run start:prod
