.PHONY: build clean run-controller run-client test help

# Build configuration
BINARY_DIR = bin
CONTROLLER_BINARY = $(BINARY_DIR)/controller
CLIENT_BINARY = $(BINARY_DIR)/client

# Default values
CONTROLLER_PORT = 8080
WEB_PORT = 8081
DATA_DIR = ./data

# Colors for output
GREEN = \033[0;32m
YELLOW = \033[1;33m
RED = \033[0;31m
NC = \033[0m # No Color

help: ## Show this help message
	@echo "$(GREEN)Available commands:$(NC)"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(YELLOW)%-15s$(NC) %s\n", $$1, $$2}'

build: ## Build both controller and client binaries for current platform
	@echo "$(GREEN)Building binaries for current platform...$(NC)"
	@mkdir -p $(BINARY_DIR)
	@go build -o $(CONTROLLER_BINARY) cmd/controller/main.go
	@go build -o $(CLIENT_BINARY) cmd/client/main.go
	@echo "$(GREEN)Build complete!$(NC)"

build-all: ## Build clients for all platforms (Windows, Linux, macOS)
	@echo "$(GREEN)Building clients for all platforms...$(NC)"
	@chmod +x scripts/build-all.sh
	@./scripts/build-all.sh

build-controller: ## Build only the controller binary
	@echo "$(GREEN)Building controller...$(NC)"
	@mkdir -p $(BINARY_DIR)
	@go build -o $(CONTROLLER_BINARY) cmd/controller/main.go
	@echo "$(GREEN)Controller build complete!$(NC)"

build-client: ## Build only the client binary for current platform
	@echo "$(GREEN)Building client for current platform...$(NC)"
	@mkdir -p $(BINARY_DIR)
	@go build -o $(CLIENT_BINARY) cmd/client/main.go
	@echo "$(GREEN)Client build complete!$(NC)"

build-client-windows: ## Build Windows client
	@echo "$(GREEN)Building Windows client...$(NC)"
	@mkdir -p $(BINARY_DIR)
	@GOOS=windows GOARCH=amd64 go build -tags windows -o $(BINARY_DIR)/tailscale-client-windows-amd64.exe cmd/client_windows/main.go
	@echo "$(GREEN)Windows client build complete!$(NC)"

build-client-linux: ## Build Linux client
	@echo "$(GREEN)Building Linux client...$(NC)"
	@mkdir -p $(BINARY_DIR)
	@GOOS=linux GOARCH=amd64 go build -tags linux -o $(BINARY_DIR)/tailscale-client-linux-amd64 cmd/client_linux/main.go
	@echo "$(GREEN)Linux client build complete!$(NC)"

build-client-darwin: ## Build macOS client
	@echo "$(GREEN)Building macOS client...$(NC)"
	@mkdir -p $(BINARY_DIR)
	@GOOS=darwin GOARCH=amd64 go build -tags darwin -o $(BINARY_DIR)/tailscale-client-darwin-amd64 cmd/client_darwin/main.go
	@echo "$(GREEN)macOS client build complete!$(NC)"

clean: ## Clean build artifacts
	@echo "$(GREEN)Cleaning build artifacts...$(NC)"
	@rm -rf $(BINARY_DIR)
	@go clean
	@echo "$(GREEN)Clean complete!$(NC)"

run-controller: build-controller ## Run the controller
	@echo "$(GREEN)Starting controller on port $(CONTROLLER_PORT)...$(NC)"
	@echo "$(YELLOW)Web interface will be available at: http://localhost:$(WEB_PORT)$(NC)"
	@sudo $(CONTROLLER_BINARY) -port $(CONTROLLER_PORT) -web-port $(WEB_PORT) -data-dir $(DATA_DIR)

run-client: build-client ## Run a client (requires NODE_NAME parameter)
	@if [ -z "$(NODE_NAME)" ]; then \
		echo "$(RED)Error: NODE_NAME is required$(NC)"; \
		echo "Usage: make run-client NODE_NAME=my-node"; \
		exit 1; \
	fi
	@echo "$(GREEN)Starting client '$(NODE_NAME)'...$(NC)"
	@sudo $(CLIENT_BINARY) -name $(NODE_NAME) -controller localhost:$(CONTROLLER_PORT)

run-subnet-router: build-client ## Run a subnet router (requires NODE_NAME and SUBNETS parameters)
	@if [ -z "$(NODE_NAME)" ]; then \
		echo "$(RED)Error: NODE_NAME is required$(NC)"; \
		echo "Usage: make run-subnet-router NODE_NAME=gateway SUBNETS=192.168.1.0/24"; \
		exit 1; \
	fi
	@if [ -z "$(SUBNETS)" ]; then \
		echo "$(RED)Error: SUBNETS is required$(NC)"; \
		echo "Usage: make run-subnet-router NODE_NAME=gateway SUBNETS=192.168.1.0/24"; \
		exit 1; \
	fi
	@echo "$(GREEN)Starting subnet router '$(NODE_NAME)' advertising $(SUBNETS)...$(NC)"
	@sudo $(CLIENT_BINARY) -name $(NODE_NAME) -controller localhost:$(CONTROLLER_PORT) -subnet-router -subnets $(SUBNETS)

test: ## Run tests
	@echo "$(GREEN)Running tests...$(NC)"
	@go test -v ./...

test-coverage: ## Run tests with coverage
	@echo "$(GREEN)Running tests with coverage...$(NC)"
	@go test -v -cover ./...

deps: ## Download dependencies
	@echo "$(GREEN)Downloading dependencies...$(NC)"
	@go mod download
	@go mod tidy

install-wireguard: ## Install WireGuard (Ubuntu/Debian)
	@echo "$(GREEN)Installing WireGuard...$(NC)"
	@sudo apt update
	@sudo apt install -y wireguard wireguard-tools
	@echo "$(GREEN)WireGuard installation complete!$(NC)"

setup: deps build ## Setup the project (download deps and build)
	@echo "$(GREEN)Setup complete!$(NC)"

dev-controller: ## Run controller in development mode with hot reload
	@echo "$(GREEN)Starting controller in development mode...$(NC)"
	@echo "$(YELLOW)Install air for hot reload: go install github.com/cosmtrek/air@latest$(NC)"
	@if command -v air > /dev/null; then \
		air -c .air.toml; \
	else \
		echo "$(RED)Air not found. Install with: go install github.com/cosmtrek/air@latest$(NC)"; \
		make run-controller; \
	fi

# Example usage targets
example-setup: ## Run a complete example setup (controller + 2 clients)
	@echo "$(GREEN)Setting up example network...$(NC)"
	@echo "$(YELLOW)This will start a controller and 2 clients in separate terminals$(NC)"
	@echo "$(YELLOW)Press Ctrl+C to stop all processes$(NC)"
	@echo ""
	@echo "$(GREEN)Starting controller...$(NC)"
	@gnome-terminal --title="Controller" -- make run-controller &
	@sleep 3
	@echo "$(GREEN)Starting client 1...$(NC)"
	@gnome-terminal --title="Client 1" -- make run-client NODE_NAME=laptop &
	@sleep 2
	@echo "$(GREEN)Starting client 2...$(NC)"
	@gnome-terminal --title="Client 2" -- make run-client NODE_NAME=desktop &
	@echo "$(GREEN)Example setup complete!$(NC)"
	@echo "$(YELLOW)Web interface: http://localhost:$(WEB_PORT)$(NC)"
	@echo "$(YELLOW)Press Ctrl+C to stop all processes$(NC)"
	@wait

# Docker targets (if needed)
docker-build: ## Build Docker images
	@echo "$(GREEN)Building Docker images...$(NC)"
	@docker build -t tailscale-clone-controller -f Dockerfile.controller .
	@docker build -t tailscale-clone-client -f Dockerfile.client .

docker-run-controller: ## Run controller in Docker
	@echo "$(GREEN)Running controller in Docker...$(NC)"
	@docker run --rm -p $(CONTROLLER_PORT):$(CONTROLLER_PORT) -p $(WEB_PORT):$(WEB_PORT) \
		-v $(PWD)/data:/app/data tailscale-clone-controller

# Utility targets
check-wireguard: ## Check if WireGuard is available
	@echo "$(GREEN)Checking WireGuard availability...$(NC)"
	@if command -v wg > /dev/null; then \
		echo "$(GREEN)WireGuard is installed$(NC)"; \
	else \
		echo "$(RED)WireGuard is not installed$(NC)"; \
		echo "$(YELLOW)Run 'make install-wireguard' to install$(NC)"; \
	fi

check-privileges: ## Check if running with sufficient privileges
	@echo "$(GREEN)Checking privileges...$(NC)"
	@if [ "$$(id -u)" -eq 0 ]; then \
		echo "$(GREEN)Running as root$(NC)"; \
	else \
		echo "$(YELLOW)Not running as root - some operations may fail$(NC)"; \
		echo "$(YELLOW)Use 'sudo make' for operations that require root$(NC)"; \
	fi

status: ## Show current status
	@echo "$(GREEN)Current status:$(NC)"
	@echo "  Controller port: $(CONTROLLER_PORT)"
	@echo "  Web interface port: $(WEB_PORT)"
	@echo "  Data directory: $(DATA_DIR)"
	@if [ -f "$(CONTROLLER_BINARY)" ]; then \
		echo "  Controller binary: $(GREEN)✓$(NC)"; \
	else \
		echo "  Controller binary: $(RED)✗$(NC)"; \
	fi
	@if [ -f "$(CLIENT_BINARY)" ]; then \
		echo "  Client binary: $(GREEN)✓$(NC)"; \
	else \
		echo "  Client binary: $(RED)✗$(NC)"; \
	fi

# Default target
.DEFAULT_GOAL := help 