.PHONY: help build run run-all stop clean test docker-build docker-up docker-down

# Default target
help:
	@echo "E-Commerce Platform - Makefile Commands"
	@echo ""
	@echo "Development:"
	@echo "  make build          - Build all services"
	@echo "  make run-all        - Run all services locally (in background)"
	@echo "  make run-gateway    - Run API Gateway"
	@echo "  make run-product    - Run Product Service"
	@echo "  make run-user       - Run User Service"
	@echo "  make run-cart       - Run Cart Service"
	@echo "  make run-order      - Run Order Service"
	@echo "  make stop           - Stop all running services"
	@echo ""
	@echo "Docker:"
	@echo "  make docker-build   - Build Docker images"
	@echo "  make docker-up      - Start all services with Docker Compose"
	@echo "  make docker-down    - Stop all Docker services"
	@echo "  make docker-logs    - View Docker logs"
	@echo ""
	@echo "Testing:"
	@echo "  make test           - Run all tests"
	@echo "  make test-api       - Test API endpoints (requires services running)"
	@echo ""
	@echo "Cleanup:"
	@echo "  make clean          - Clean build artifacts"

# Build all services
build:
	@echo "Building all services..."
	@go build -o bin/gateway ./cmd/gateway
	@go build -o bin/product ./cmd/product
	@go build -o bin/user ./cmd/user
	@go build -o bin/cart ./cmd/cart
	@go build -o bin/order ./cmd/order
	@echo "Build complete!"

# Run individual services
run-gateway: build
	@echo "Starting API Gateway on port 8080..."
	@./bin/gateway

run-product: build
	@echo "Starting Product Service on port 8081..."
	@./bin/product

run-user: build
	@echo "Starting User Service on port 8082..."
	@./bin/user

run-cart: build
	@echo "Starting Cart Service on port 8083..."
	@./bin/cart

run-order: build
	@echo "Starting Order Service on port 8084..."
	@./bin/order

# Run all services (requires separate terminals)
run-all: build
	@echo "Starting all services..."
	@echo "Note: This requires 5 separate terminals. Use 'make docker-up' for easier deployment."
	@echo ""
	@echo "Terminal 1: make run-product"
	@echo "Terminal 2: make run-user"
	@echo "Terminal 3: make run-cart"
	@echo "Terminal 4: make run-order"
	@echo "Terminal 5: make run-gateway"

# Docker commands
docker-build:
	@echo "Building Docker images..."
	@docker-compose build

docker-up:
	@echo "Starting services with Docker Compose..."
	@docker-compose up -d
	@echo ""
	@echo "Services started! Access the API at http://localhost:8080"
	@echo "Use 'make docker-logs' to view logs"

docker-down:
	@echo "Stopping Docker services..."
	@docker-compose down

docker-logs:
	@docker-compose logs -f

# Testing
test:
	@echo "Running tests..."
	@go test -v ./...

test-api:
	@echo "Testing API endpoints..."
	@echo ""
	@echo "1. Testing health checks..."
	@curl -s http://localhost:8080/health | jq . || echo "Gateway not responding"
	@echo ""
	@echo "2. Testing product list..."
	@curl -s http://localhost:8080/api/products | jq . || echo "Products endpoint not responding"
	@echo ""
	@echo "For full API testing, see README.md"

# Cleanup
clean:
	@echo "Cleaning build artifacts..."
	@rm -rf bin/
	@go clean
	@echo "Clean complete!"

stop:
	@echo "Stopping services..."
	@pkill -f "bin/gateway" || true
	@pkill -f "bin/product" || true
	@pkill -f "bin/user" || true
	@pkill -f "bin/cart" || true
	@pkill -f "bin/order" || true
	@echo "Services stopped!"
