#!/bin/bash

# Cube Database - Docker Helper Script

set -e

GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'

print_help() {
    echo "Cube Database - Docker Helper"
    echo ""
    echo "Usage: $0 <command>"
    echo ""
    echo "Commands:"
    echo "  build          Build Docker image"
    echo "  start          Start 3-node cluster"
    echo "  stop           Stop cluster"
    echo "  restart        Restart cluster"
    echo "  status         Show cluster status"
    echo "  logs           Show logs (all nodes)"
    echo "  logs-node <N>  Show logs for node N"
    echo "  shell-node <N> Open shell in node N"
    echo "  clean          Stop and remove all containers and volumes"
    echo "  test           Run test queries"
    echo ""
}

print_step() {
    echo -e "${BLUE}▶ $1${NC}"
}

print_success() {
    echo -e "${GREEN}✓ $1${NC}"
}

case "$1" in
    build)
        print_step "Building Cube database image..."
        docker build -t cube-db:latest .
        print_success "Image built successfully"
        ;;
        
    start)
        print_step "Starting 3-node Cube database cluster..."
        docker-compose up -d
        echo ""
        print_step "Waiting for nodes to be healthy..."
        sleep 10
        docker-compose ps
        echo ""
        print_success "Cluster started!"
        echo ""
        echo "Access nodes at:"
        echo "  Node 1: http://localhost:8080"
        echo "  Node 2: http://localhost:8081"
        echo "  Node 3: http://localhost:8082"
        ;;
        
    stop)
        print_step "Stopping cluster..."
        docker-compose stop
        print_success "Cluster stopped"
        ;;
        
    restart)
        print_step "Restarting cluster..."
        docker-compose restart
        print_success "Cluster restarted"
        ;;
        
    status)
        print_step "Cluster status:"
        docker-compose ps
        echo ""
        print_step "Node health:"
        for port in 8080 8081 8082; do
            echo -n "Node on port $port: "
            if curl -s -f http://localhost:$port/api/v1/health > /dev/null 2>&1; then
                echo -e "${GREEN}✓ Healthy${NC}"
            else
                echo -e "${YELLOW}✗ Unhealthy${NC}"
            fi
        done
        ;;
        
    logs)
        docker-compose logs -f
        ;;
        
    logs-node)
        if [ -z "$2" ]; then
            echo "Usage: $0 logs-node <node-number>"
            exit 1
        fi
        docker logs -f cube-node-$2
        ;;
        
    shell-node)
        if [ -z "$2" ]; then
            echo "Usage: $0 shell-node <node-number>"
            exit 1
        fi
        docker exec -it cube-node-$2 /bin/bash
        ;;
        
    clean)
        print_step "Stopping and removing all containers and volumes..."
        docker-compose down -v
        print_success "Cleanup complete"
        ;;
        
    test)
        print_step "Testing cluster..."
        
        echo "1. Writing data to node 1..."
        curl -s -X POST http://localhost:8080/api/v1/put \
          -H "Content-Type: application/json" \
          -d '{"key":"test:docker","value":"Hello from Docker!"}' | jq
        
        echo ""
        echo "2. Reading from node 2..."
        curl -s http://localhost:8081/api/v1/get/test:docker | jq
        
        echo ""
        echo "3. Reading from node 3..."
        curl -s http://localhost:8082/api/v1/get/test:docker | jq
        
        echo ""
        print_success "Test complete"
        ;;
        
    *)
        print_help
        exit 1
        ;;
esac
