.PHONY: help build test clean run docker-build docker-run dev-up dev-down lint fmt vet

# Default target
help: ## Show this help message
	@echo 'Usage: make [target]'
	@echo ''
	@echo 'Targets:'
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "  %-15s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

# Build Configuration
BINARY_NAME=tm-api
MAIN_PATH=./cmd/api/main.go
BUILD_DIR=./bin

# Go Configuration
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod
GOFMT=gofmt
GOVET=$(GOCMD) vet

# Build flags
LDFLAGS=-ldflags "-w -s"
BUILD_FLAGS=-a -installsuffix cgo

# Development
dev-up: ## Start development environment with Docker Compose
	docker-compose up -d mongodb redis rabbitmq elasticsearch
	@echo "Development services started. Waiting for services to be ready..."
	@sleep 10
	@echo "Services should be ready. Run 'make run' to start the API."

dev-down: ## Stop development environment
	docker-compose down
	docker-compose down --volumes --remove-orphans

dev-logs: ## Show logs from development services
	docker-compose logs -f

dev-status: ## Show status of development services
	docker-compose ps

# Building
build: ## Build the application
	mkdir -p $(BUILD_DIR)
	CGO_ENABLED=0 GOOS=linux $(GOBUILD) $(LDFLAGS) $(BUILD_FLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PATH)

build-local: ## Build for local development
	mkdir -p $(BUILD_DIR)
	$(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PATH)

# Running
run: ## Run the application locally
	$(GOCMD) run $(MAIN_PATH)

run-binary: build-local ## Build and run the binary
	$(BUILD_DIR)/$(BINARY_NAME)

# Testing
test: ## Run tests
	$(GOTEST) -v ./...

test-coverage: ## Run tests with coverage
	$(GOTEST) -v -coverprofile=coverage.out ./...
	$(GOCMD) tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report generated: coverage.html"

test-race: ## Run tests with race detection
	$(GOTEST) -race -v ./...

benchmark: ## Run benchmarks
	$(GOTEST) -bench=. -benchmem ./...

# Code Quality
lint: ## Run golangci-lint
	golangci-lint run

fmt: ## Format Go code
	$(GOFMT) -s -w .
	$(GOCMD) fmt ./...

vet: ## Run go vet
	$(GOVET) ./...

check: fmt vet lint test ## Run all code quality checks

# Dependencies
deps: ## Download dependencies
	$(GOMOD) download

deps-update: ## Update dependencies
	$(GOMOD) tidy
	$(GOGET) -u ./...
	$(GOMOD) tidy

deps-vendor: ## Vendor dependencies
	$(GOMOD) vendor

# Docker
docker-build: ## Build Docker image
	docker build -t $(BINARY_NAME):latest .

docker-run: ## Run Docker container
	docker run -p 8080:8080 --name $(BINARY_NAME) $(BINARY_NAME):latest

docker-stop: ## Stop Docker container
	docker stop $(BINARY_NAME) || true
	docker rm $(BINARY_NAME) || true

docker-push: ## Push Docker image (requires DOCKER_REGISTRY env var)
	@if [ -z "$(DOCKER_REGISTRY)" ]; then \
		echo "Error: DOCKER_REGISTRY environment variable is not set"; \
		exit 1; \
	fi
	docker tag $(BINARY_NAME):latest $(DOCKER_REGISTRY)/$(BINARY_NAME):latest
	docker push $(DOCKER_REGISTRY)/$(BINARY_NAME):latest

# Docker Compose
compose-up: ## Start all services with Docker Compose
	docker-compose up --build

compose-up-d: ## Start all services in background
	docker-compose up -d --build

compose-down: ## Stop all services
	docker-compose down

compose-logs: ## Show logs from all services
	docker-compose logs -f

compose-restart: ## Restart all services
	docker-compose restart

# Database
db-migrate: ## Run database migrations (TODO: implement)
	@echo "Database migration not implemented yet"

db-seed: ## Seed database with test data (TODO: implement)
	@echo "Database seeding not implemented yet"

db-reset: ## Reset database (TODO: implement)
	@echo "Database reset not implemented yet"

# Monitoring
logs: ## Show application logs (when running locally)
	tail -f ./logs/app.log

logs-live: ## Follow live application logs
	tail -f ./logs/app.log

logs-clean: ## Clean application logs
	rm -f ./logs/*.log
	rm -f ./logs/*.log.gz

health: ## Check application health
	curl -f http://localhost:8080/health || echo "Health check failed"

# Cleanup
clean: ## Clean build artifacts
	$(GOCLEAN)
	rm -rf $(BUILD_DIR)
	rm -f coverage.out coverage.html

clean-all: logs-clean clean ## Clean everything including logs
	@echo "All artifacts cleaned"

clean-docker: ## Clean Docker images and containers
	docker-compose down --volumes --remove-orphans
	docker system prune -af

# Release (TODO: implement proper versioning)
release: check build docker-build ## Build release version
	@echo "Release build completed"

# Production deployment (example)
deploy-staging: ## Deploy to staging environment
	@echo "Deploying to staging..."
	# Add your staging deployment commands here

deploy-prod: ## Deploy to production environment
	@echo "Deploying to production..."
	# Add your production deployment commands here

# Generate documentation
docs: ## Generate API documentation
	@echo "Generating API documentation..."
	# Add swagger/openapi generation here

# Security
security-scan: ## Run security scan on dependencies
	$(GOCMD) list -json -deps ./... | nancy sleuth

# Installation
install: ## Install the application globally
	$(GOCMD) install $(MAIN_PATH)

# Environment setup for new developers
setup: ## Setup development environment
	@echo "Setting up development environment..."
	$(GOMOD) download
	@echo "Installing development tools..."
	$(GOCMD) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
	@echo "Setup completed! Run 'make dev-up' to start services and 'make run' to start the API."

# Show build information
info: ## Show build information
	@echo "Binary Name: $(BINARY_NAME)"
	@echo "Main Path: $(MAIN_PATH)"
	@echo "Build Directory: $(BUILD_DIR)"
	@echo "Go Version: $(shell $(GOCMD) version)"
	@echo "Git Commit: $(shell git rev-parse --short HEAD 2>/dev/null || echo 'N/A')"
	@echo "Build Time: $(shell date)"

# All-in-one development start
dev: deps dev-up ## Setup and start complete development environment
	@echo "Development environment is ready!"
	@echo "API will be available at: http://localhost:8080"
	@echo "MongoDB: localhost:27017"
	@echo "Redis: localhost:6379" 
	@echo "RabbitMQ Management: http://localhost:15672 (admin/password)"
	@echo "Elasticsearch: http://localhost:9200"
	@echo "Kibana: http://localhost:5601" 