#!/bin/bash
# =============================================================================
# Telemetry Service - 独立部署脚本
# =============================================================================

set -e

SERVICE_NAME="telemetry-service"
CONTAINER_NAME="genex-telemetry-service"
IMAGE_NAME="backend-telemetry-service"
PORT=3011
HEALTH_ENDPOINT="/api/v1/health"

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

log_info()    { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn()    { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error()   { echo -e "${RED}[ERROR]${NC} $1"; }

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="$(dirname "$(dirname "$SCRIPT_DIR")")"
COMPOSE_FILE="$BACKEND_DIR/docker-compose.yml"

compose() {
    docker compose -f "$COMPOSE_FILE" "$@"
}

case "$1" in
    build)
        log_info "增量编译 $SERVICE_NAME..."
        compose build "$SERVICE_NAME"
        log_success "$SERVICE_NAME 编译完成"
        ;;
    build-no-cache)
        log_info "全新编译 $SERVICE_NAME (无缓存)..."
        compose build --no-cache "$SERVICE_NAME"
        log_success "$SERVICE_NAME 全新编译完成"
        ;;
    start)
        log_info "启动 $SERVICE_NAME..."
        compose up -d "$SERVICE_NAME"
        log_success "$SERVICE_NAME 已启动"
        ;;
    stop)
        log_info "停止 $SERVICE_NAME..."
        compose stop "$SERVICE_NAME"
        log_success "$SERVICE_NAME 已停止"
        ;;
    restart)
        log_info "重启 $SERVICE_NAME..."
        compose stop "$SERVICE_NAME"
        compose up -d "$SERVICE_NAME"
        log_success "$SERVICE_NAME 已重启"
        ;;
    rebuild)
        log_info "增量编译并重启 $SERVICE_NAME..."
        compose build "$SERVICE_NAME"
        compose up -d "$SERVICE_NAME"
        log_success "$SERVICE_NAME 编译并重启完成"
        ;;
    rebuild-no-cache)
        log_info "全新编译并重启 $SERVICE_NAME..."
        compose build --no-cache "$SERVICE_NAME"
        compose up -d "$SERVICE_NAME"
        log_success "$SERVICE_NAME 全新编译并重启完成"
        ;;
    logs)
        docker logs -f "$CONTAINER_NAME"
        ;;
    logs-tail)
        docker logs --tail 100 "$CONTAINER_NAME"
        ;;
    status)
        if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
            log_success "$SERVICE_NAME 运行中"
            docker ps --filter "name=$CONTAINER_NAME" --format "table {{.Status}}\t{{.Ports}}"
        else
            log_warn "$SERVICE_NAME 未运行"
        fi
        ;;
    health)
        log_info "检查 $SERVICE_NAME 健康状态..."
        if curl -sf "http://localhost:$PORT$HEALTH_ENDPOINT" > /dev/null 2>&1; then
            log_success "$SERVICE_NAME 健康"
        else
            log_error "$SERVICE_NAME 健康检查失败"
            exit 1
        fi
        ;;
    shell)
        log_info "进入 $SERVICE_NAME 容器..."
        docker exec -it "$CONTAINER_NAME" sh
        ;;
    test)
        log_info "运行 $SERVICE_NAME 测试..."
        cd "$SCRIPT_DIR"
        npm test
        ;;
    *)
        echo "$SERVICE_NAME 独立部署脚本"
        echo ""
        echo "用法: $0 <命令>"
        echo ""
        echo "命令:"
        echo "  build            增量编译 Docker 镜像"
        echo "  build-no-cache   全新编译 (无缓存)"
        echo "  start            启动服务"
        echo "  stop             停止服务"
        echo "  restart          重启服务"
        echo "  rebuild          增量编译并重启"
        echo "  rebuild-no-cache 全新编译并重启"
        echo "  logs             实时跟踪日志"
        echo "  logs-tail        查看最近 100 行日志"
        echo "  status           查看服务状态"
        echo "  health           健康检查"
        echo "  shell            进入容器 Shell"
        echo "  test             运行本地测试"
        exit 1
        ;;
esac
