.PHONY: help init dev build test test-unit test-integration test-bdd test-coverage clean docs-clean docker-up docker-down docker-logs migrate-up migrate-down migrate-create migrate-version sqlc-generate lint format docs-serve bdd-docs docs-pdf

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

## help: Display this help message
help:
	@echo "$(BLUE)RoadWave - Makefile Commands$(NC)"
	@echo ""
	@grep -E '^## ' $(MAKEFILE_LIST) | sed 's/^## /  /'
	@echo ""

## init: Initialize project (install tools, setup env)
init:
	@echo "$(GREEN)Initializing project...$(NC)"
	@go install github.com/cosmtrek/air@latest
	@go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
	@go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
	@go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
	@cp backend/.env.example backend/.env
	@echo "$(GREEN)✓ Project initialized$(NC)"

## dev: Start development environment with hot reload
dev:
	@echo "$(BLUE)Starting development server...$(NC)"
	@cd backend && air -c .air.toml

## build: Build production binary
build:
	@echo "$(BLUE)Building production binary...$(NC)"
	@cd backend && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o bin/api ./cmd/api
	@echo "$(GREEN)✓ Binary built: backend/bin/api$(NC)"

## test: Run all tests
test: test-unit test-integration test-bdd

## test-unit: Run unit tests
test-unit:
	@echo "$(BLUE)Running unit tests...$(NC)"
	@cd backend && go test -v -race -short ./...

## test-integration: Run integration tests
test-integration:
	@echo "$(BLUE)Running integration tests...$(NC)"
	@cd backend && go test -v -race -tags=integration ./...

## test-bdd: Run BDD tests (Godog)
test-bdd:
	@echo "$(BLUE)Running BDD tests...$(NC)"
	@godog run features/

## test-coverage: Run tests with coverage report
test-coverage:
	@echo "$(BLUE)Running tests with coverage...$(NC)"
	@cd backend && go test -race -coverprofile=coverage.out -covermode=atomic ./...
	@cd backend && go tool cover -html=coverage.out -o coverage.html
	@cd backend && go tool cover -func=coverage.out | grep total | awk '{print "Total coverage: " $$3}'
	@echo "$(GREEN)✓ Coverage report: backend/coverage.html$(NC)"

## clean: Clean build artifacts and temporary files
clean:
	@echo "$(YELLOW)Cleaning...$(NC)"
	@rm -rf bin/ tmp/ coverage.out coverage.html
	@echo "$(GREEN)✓ Cleaned$(NC)"

## docs-clean: Remove generated documentation (BDD docs and PDF)
docs-clean:
	@echo "$(YELLOW)Cleaning generated documentation...$(NC)"
	@rm -rf docs/bdd/ output/RoadWave_Documentation.pdf
	@docker rmi roadwave-pdf-generator 2>/dev/null || true
	@echo "$(GREEN)✓ Documentation cleaned$(NC)"

## docker-up: Start all Docker services
docker-up:
	@echo "$(BLUE)Starting Docker services...$(NC)"
	@docker compose up -d
	@echo "$(GREEN)✓ Services started$(NC)"
	@echo "$(YELLOW)API:      http://localhost:8080$(NC)"
	@echo "$(YELLOW)Zitadel:  http://localhost:8081$(NC)"
	@echo "$(YELLOW)Adminer:  http://localhost:8082$(NC)"

## docker-down: Stop all Docker services
docker-down:
	@echo "$(YELLOW)Stopping Docker services...$(NC)"
	@docker compose down
	@echo "$(GREEN)✓ Services stopped$(NC)"

## docker-logs: Show Docker logs
docker-logs:
	@docker compose logs -f

## migrate-up: Apply all migrations
migrate-up:
	@echo "$(BLUE)Applying migrations...$(NC)"
	@migrate -path backend/migrations -database "postgres://roadwave:dev_password@localhost:5432/roadwave_dev?sslmode=disable" up
	@echo "$(GREEN)✓ Migrations applied$(NC)"

## migrate-down: Rollback last migration
migrate-down:
	@echo "$(YELLOW)Rolling back last migration...$(NC)"
	@migrate -path backend/migrations -database "postgres://roadwave:dev_password@localhost:5432/roadwave_dev?sslmode=disable" down 1
	@echo "$(GREEN)✓ Migration rolled back$(NC)"

## migrate-create: Create new migration (usage: make migrate-create name=add_users)
migrate-create:
	@if [ -z "$(name)" ]; then \
		echo "$(RED)Error: name parameter required$(NC)"; \
		echo "Usage: make migrate-create name=add_users"; \
		exit 1; \
	fi
	@migrate create -ext sql -dir backend/migrations -seq $(name)
	@echo "$(GREEN)✓ Migration created$(NC)"

## migrate-version: Show current migration version
migrate-version:
	@migrate -path backend/migrations -database "postgres://roadwave:dev_password@localhost:5432/roadwave_dev?sslmode=disable" version

## sqlc-generate: Generate Go code from SQL queries
sqlc-generate:
	@echo "$(BLUE)Generating Go code from SQL...$(NC)"
	@cd backend && sqlc generate
	@echo "$(GREEN)✓ Code generated$(NC)"

## lint: Run linter
lint:
	@echo "$(BLUE)Running linter...$(NC)"
	@cd backend && golangci-lint run ./...

## format: Format code
format:
	@echo "$(BLUE)Formatting code...$(NC)"
	@cd backend && go fmt ./...
	@cd backend && gofmt -s -w .
	@echo "$(GREEN)✓ Code formatted$(NC)"

## deps: Download dependencies
deps:
	@echo "$(BLUE)Downloading dependencies...$(NC)"
	@cd backend && go mod download
	@cd backend && go mod tidy
	@echo "$(GREEN)✓ Dependencies updated$(NC)"

## run-api: Run API server (without hot reload)
run-api:
	@echo "$(BLUE)Starting API server...$(NC)"
	@cd backend && go run cmd/api/main.go

## docs-serve: Start documentation server (http://localhost:8000)
docs-serve:
	@echo "$(BLUE)Generating BDD documentation from Gherkin files...$(NC)"
	@python3 scripts/generate-bdd-docs.py
	@echo "$(GREEN)✓ BDD documentation generated$(NC)"
	@echo "$(BLUE)Starting documentation server...$(NC)"
	@echo "$(YELLOW)Documentation available at http://localhost:8000$(NC)"
	@docker run --rm -it -p 8000:8000 -v "$(PWD):/docs" squidfunk/mkdocs-material

## bdd-docs: Generate BDD docs from Gherkin and serve with MkDocs (http://localhost:8000)
bdd-docs:
	@echo "$(BLUE)Generating BDD documentation from Gherkin files...$(NC)"
	@python3 scripts/generate-bdd-docs.py
	@echo "$(GREEN)✓ BDD documentation generated$(NC)"
	@echo "$(BLUE)Starting documentation server...$(NC)"
	@echo "$(YELLOW)Documentation available at http://localhost:8000$(NC)"
	@echo "$(YELLOW)Navigate to 'Tests BDD' section$(NC)"
	@docker run --rm -it -p 8000:8000 -v "$(PWD):/docs" squidfunk/mkdocs-material

## docs-pdf: Generate PDF of all documentation (output/RoadWave_Documentation.pdf)
docs-pdf:
	@echo "$(BLUE)Generating BDD documentation from Gherkin files...$(NC)"
	@python3 scripts/generate-bdd-docs.py
	@echo "$(GREEN)✓ BDD documentation generated$(NC)"
	@echo "$(BLUE)Building PDF generator Docker image...$(NC)"
	@docker build -t roadwave-pdf-generator -f scripts/Dockerfile.pdf . -q
	@echo "$(BLUE)Generating PDF documentation...$(NC)"
	@docker run --rm -u $(shell id -u):$(shell id -g) -v "$(PWD):/docs" roadwave-pdf-generator
	@echo "$(GREEN)✓ PDF generated: output/RoadWave_Documentation.pdf$(NC)"
