.PHONY: help build run test clean docker-build docker-run deploy lint fmt

# Variables
APP_NAME=stream-processor
DOCKER_IMAGE=synthoraai/stream-processor
VERSION?=latest
GO_VERSION=1.21

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

install: ## Install dependencies
	@echo "Installing dependencies..."
	@go mod download
	@go mod verify

build: ## Build the application
	@echo "Building $(APP_NAME)..."
	@cd cmd/stream-processor && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o ../../bin/$(APP_NAME) .

run: ## Run the application
	@echo "Running $(APP_NAME)..."
	@go run cmd/stream-processor/main.go

test: ## Run tests
	@echo "Running tests..."
	@go test -v -race -coverprofile=coverage.out ./...
	@go tool cover -html=coverage.out -o coverage.html

test-integration: ## Run integration tests
	@echo "Running integration tests..."
	@go test -v -tags=integration ./...

benchmark: ## Run benchmarks
	@echo "Running benchmarks..."
	@go test -bench=. -benchmem ./...

lint: ## Run linter
	@echo "Running linter..."
	@golangci-lint run ./...

fmt: ## Format code
	@echo "Formatting code..."
	@gofmt -s -w .
	@go mod tidy

clean: ## Clean build artifacts
	@echo "Cleaning..."
	@rm -rf bin/
	@rm -f coverage.out coverage.html

docker-build: ## Build Docker image
	@echo "Building Docker image..."
	@docker build -t $(DOCKER_IMAGE):$(VERSION) .
	@docker tag $(DOCKER_IMAGE):$(VERSION) $(DOCKER_IMAGE):latest

docker-run: ## Run Docker container
	@echo "Running Docker container..."
	@docker-compose up -d

docker-stop: ## Stop Docker container
	@echo "Stopping Docker container..."
	@docker-compose down

docker-logs: ## View Docker logs
	@docker-compose logs -f stream-processor

docker-push: ## Push Docker image
	@echo "Pushing Docker image..."
	@docker push $(DOCKER_IMAGE):$(VERSION)
	@docker push $(DOCKER_IMAGE):latest

k8s-deploy: ## Deploy to Kubernetes
	@echo "Deploying to Kubernetes..."
	@kubectl apply -f k8s/

k8s-delete: ## Delete from Kubernetes
	@echo "Deleting from Kubernetes..."
	@kubectl delete -f k8s/

k8s-logs: ## View Kubernetes logs
	@kubectl logs -f deployment/stream-processor

dev: ## Run in development mode
	@echo "Starting development environment..."
	@docker-compose -f docker-compose.yml up

prod: ## Run in production mode
	@echo "Starting production environment..."
	@docker-compose -f docker-compose.prod.yml up -d

monitoring: ## Start monitoring stack
	@echo "Starting monitoring stack..."
	@docker-compose -f docker-compose.monitoring.yml up -d

generate: ## Generate code
	@echo "Generating code..."
	@go generate ./...

migrate-up: ## Run database migrations up
	@echo "Running migrations up..."
	# Add migration commands here

migrate-down: ## Run database migrations down
	@echo "Running migrations down..."
	# Add migration commands here

proto: ## Generate protobuf code
	@echo "Generating protobuf code..."
	@protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/*.proto

verify: ## Verify dependencies
	@echo "Verifying dependencies..."
	@go mod verify

security: ## Run security checks
	@echo "Running security checks..."
	@gosec ./...

all: clean fmt lint test build ## Run all checks and build

.DEFAULT_GOAL := help
