#!/bin/bash
# ==============================================================
# Nacos 启动脚本
# 支持单机/集群模式
# ==============================================================

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 默认配置
MODE=${MODE:-standalone}
NACOS_VERSION=${NACOS_VERSION:-v2.2.3}
NACOS_SERVER_ADDR=${NACOS_SERVER_ADDR:-127.0.0.1:8848}
NACOS_NAMESPACE=${NACOS_NAMESPACE:-public}
NACOS_GROUP=${NACOS_GROUP:-DEFAULT_GROUP}
MYSQL_HOST=${MYSQL_HOST:-localhost}
MYSQL_PORT=${MYSQL_PORT:-3308}

echo_color() {
    echo -e "${2}[$1]${NC} $3"
}

# 显示帮助
show_help() {
    cat << EOF
用法: $0 [OPTIONS]

选项:
    --mode MODE              运行模式: standalone | cluster (默认: standalone)
    --version VERSION        Nacos版本 (默认: v2.2.3)
    --nacos-addr ADDR        Nacos服务器地址 (默认: 127.0.0.1:8848)
    --namespace NAMESPACE    命名空间 (默认: public)
    --group GROUP            分组 (默认: DEFAULT_GROUP)
    --mysql-host HOST        MySQL主机 (默认: localhost)
    --mysql-port PORT        MySQL端口 (默认: 3308)
    -h, --help               显示帮助

示例:
    # 启动单机模式
    $0 --mode standalone

    # 启动集群模式
    $0 --mode cluster --nacos-addr 192.168.1.100:8848

    # 指定命名空间
    $0 --mode standalone --namespace dev --group DEV_GROUP
EOF
}

# 解析参数
while [[ $# -gt 0 ]]; do
    case $1 in
        --mode)
            MODE="$2"
            shift 2
            ;;
        --version)
            NACOS_VERSION="$2"
            shift 2
            ;;
        --nacos-addr)
            NACOS_SERVER_ADDR="$2"
            shift 2
            ;;
        --namespace)
            NACOS_NAMESPACE="$2"
            shift 2
            ;;
        --group)
            NACOS_GROUP="$2"
            shift 2
            ;;
        --mysql-host)
            MYSQL_HOST="$2"
            shift 2
            ;;
        --mysql-port)
            MYSQL_PORT="$2"
            shift 2
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo_color "ERROR" "$RED" "未知参数: $1"
            show_help
            exit 1
            ;;
    esac
done

# 检查Docker和Docker Compose
check_requirements() {
    if ! command -v docker &> /dev/null; then
        echo_color "ERROR" "$RED" "Docker未安装"
        exit 1
    fi
    
    if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
        echo_color "ERROR" "$RED" "Docker Compose未安装"
        exit 1
    fi
    
    echo_color "OK" "$GREEN" "环境检查通过"
}

# 启动单机模式
start_standalone() {
    echo_color "INFO" "$YELLOW" "正在启动 Nacos 单机模式..."
    echo_color "INFO" "$YELLOW" "Nacos版本: $NACOS_VERSION"
    echo_color "INFO" "$YELLOW" "服务器地址: $NACOS_SERVER_ADDR"
    echo_color "INFO" "$YELLOW" "命名空间: $NACOS_NAMESPACE"
    
    cd "$PROJECT_DIR"
    
    # 使用docker-compose
    if docker compose -f docker-compose.standalone.yml up -d 2>/dev/null; then
        echo_color "OK" "$GREEN" "Docker Compose启动成功"
    else
        echo_color "INFO" "$YELLOW" "尝试使用docker-compose命令..."
        docker-compose -f docker-compose.standalone.yml up -d
    fi
    
    echo ""
    echo_color "OK" "$GREEN" "=========================================="
    echo_color "OK" "$GREEN" "Nacos 单机模式已启动"
    echo_color "OK" "$GREEN" "=========================================="
    echo "控制台:     http://localhost:8848/nacos"
    echo "用户名:     nacos"
    echo "密码:       nacos123456"
    echo "API地址:    http://localhost:8848/nacos/v1/"
    echo ""
}

# 启动集群模式
start_cluster() {
    echo_color "INFO" "$YELLOW" "正在启动 Nacos 集群模式..."
    echo_color "INFO" "$YELLOW" "Nacos版本: $NACOS_VERSION"
    echo_color "INFO" "$YELLOW" "服务器地址: $NACOS_SERVER_ADDR"
    echo_color "INFO" "$YELLOW" "节点数: 3"
    
    cd "$PROJECT_DIR"
    
    if docker compose -f docker-compose.cluster.yml up -d 2>/dev/null; then
        echo_color "OK" "$GREEN" "Docker Compose启动成功"
    else
        echo_color "INFO" "$YELLOW" "尝试使用docker-compose命令..."
        docker-compose -f docker-compose.cluster.yml up -d
    fi
    
    echo ""
    echo_color "OK" "$GREEN" "=========================================="
    echo_color "OK" "$GREEN" "Nacos 集群模式已启动"
    echo_color "OK" "$GREEN" "=========================================="
    echo "集群节点:   nacos-server-1:8848"
    echo "            nacos-server-2:8848"
    echo "            nacos-server-3:8848"
    echo "负载均衡:   localhost:8848 (通过Nginx)"
    echo "控制台:     http://localhost:8848/nacos"
    echo "用户名:     nacos"
    echo "密码:       nacos123456"
    echo ""
}

# 停止服务
stop() {
    echo_color "INFO" "$YELLOW" "正在停止 Nacos..."
    
    cd "$PROJECT_DIR"
    
    docker compose -f docker-compose.standalone.yml down 2>/dev/null || \
    docker-compose -f docker-compose.standalone.yml down 2>/dev/null || true
    
    docker compose -f docker-compose.cluster.yml down 2>/dev/null || \
    docker-compose -f docker-compose.cluster.yml down 2>/dev/null || true
    
    echo_color "OK" "$GREEN" "Nacos已停止"
}

# 查看日志
logs() {
    cd "$PROJECT_DIR"
    
    case $MODE in
        standalone)
            docker compose -f docker-compose.standalone.yml logs -f nacos-server 2>/dev/null || \
            docker-compose -f docker-compose.standalone.yml logs -f nacos-server
            ;;
        cluster)
            docker compose -f docker-compose.cluster.yml logs -f 2>/dev/null || \
            docker-compose -f docker-compose.cluster.yml logs -f
            ;;
    esac
}

# 主逻辑
main() {
    check_requirements
    
    case $MODE in
        standalone)
            start_standalone
            ;;
        cluster)
            start_cluster
            ;;
        stop)
            stop
            ;;
        logs)
            logs
            ;;
        *)
            echo_color "ERROR" "$RED" "无效模式: $MODE"
            show_help
            exit 1
            ;;
    esac
}

# 根据参数决定执行什么
if [[ "$1" == "stop" ]] || [[ "$1" == "down" ]]; then
    stop
elif [[ "$1" == "logs" ]]; then
    logs
else
    main
fi
