#!/bin/bash
# ==========================================================
# MOMO Pro System - 系統啟動腳本 (v3.0)
# 重開機後自動啟動所有服務
# 2026-02-13 更新：
#   - n8n 和 Superset 已遷移到 K8s (tools namespace)
#   - 移除 Docker n8n/Superset 啟動
#   - 新增 K8s tools namespace 重啟
# ==========================================================

LOG_FILE="/var/log/momo_startup.log"
TELEGRAM_BOT_TOKEN="${TELEGRAM_BOT_TOKEN:-<TELEGRAM_BOT_TOKEN>}"
TELEGRAM_CHAT_ID="${TELEGRAM_CHAT_ID:-5619078117}"

log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}

notify() {
    curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
        -d chat_id="${TELEGRAM_CHAT_ID}" \
        -d text="🖥️ UAT Server 啟動通知

$1" \
        -d parse_mode="HTML" > /dev/null 2>&1 || true
}

wait_container() {
    local name=$1
    local max_wait=${2:-60}
    local count=0
    while [ $count -lt $max_wait ]; do
        if docker ps --format '{{.Names}}' | grep -q "^${name}$"; then
            STATUS=$(docker inspect --format='{{.State.Status}}' "$name" 2>/dev/null)
            if [ "$STATUS" = "running" ]; then
                log "✅ $name 運行中"
                return 0
            fi
        fi
        sleep 2
        count=$((count + 2))
    done
    log "⚠️ $name 等待超時"
    return 1
}

wait_k8s_pod() {
    local label=$1
    local namespace=$2
    local max_wait=${3:-120}

    export KUBECONFIG=/home/wooo/.kube/config

    log "等待 K8s Pod ($label) 就緒..."
    kubectl wait --for=condition=ready pod -l "$label" -n "$namespace" --timeout="${max_wait}s" 2>/dev/null
    if [ $? -eq 0 ]; then
        log "✅ K8s Pod ($label) 已就緒"
        return 0
    else
        log "⚠️ K8s Pod ($label) 等待超時"
        return 1
    fi
}

main() {
    log "=========================================="
    log "🚀 系統啟動流程開始 (v3.0)"
    log "=========================================="

    notify "🔄 UAT 系統正在啟動..."

    # 1. 確保 Docker 運行
    log "=== 1. 檢查 Docker ==="
    if ! systemctl is-active --quiet docker; then
        systemctl start docker
        sleep 10
    fi
    log "✅ Docker 服務正常"

    # 2. 啟動 Docker Registry
    log "=== 2. 啟動 Docker Registry ==="
    cd /home/wooo/registry 2>/dev/null || cd /home/wooo/devops/registry 2>/dev/null
    docker compose up -d 2>/dev/null || docker start docker-registry 2>/dev/null
    wait_container "docker-registry" 60

    # 3. 啟動 GitLab
    log "=== 3. 啟動 GitLab ==="
    docker start wooo-gitlab 2>/dev/null || true
    # GitLab 啟動較慢，不等待完全就緒

    # 4. 重啟 K8s momo namespace (主應用)
    log "=== 4. 重啟 K8s momo 應用 ==="
    export KUBECONFIG=/home/wooo/.kube/config
    kubectl rollout restart deployment/momo-app -n momo 2>/dev/null || true
    kubectl rollout restart deployment/momo-scheduler -n momo 2>/dev/null || true

    # 5. 重啟 K8s tools namespace (n8n, Superset)
    log "=== 5. 重啟 K8s tools 服務 ==="
    kubectl rollout restart deployment/n8n -n tools 2>/dev/null || true
    kubectl rollout restart deployment/superset -n tools 2>/dev/null || true

    # 6. 等待 K8s Pod 就緒
    log "=== 6. 等待 K8s Pod 就緒 ==="
    wait_k8s_pod "app=momo-app" "momo" 120
    wait_k8s_pod "app=n8n" "tools" 60
    wait_k8s_pod "app=superset" "tools" 90

    # 7. 健康檢查
    log "=== 7. 健康檢查 ==="
    FAILED=""

    # 取得 K8s ClusterIP
    N8N_IP=$(kubectl get svc n8n -n tools -o jsonpath='{.spec.clusterIP}' 2>/dev/null)
    SUPERSET_IP=$(kubectl get svc superset -n tools -o jsonpath='{.spec.clusterIP}' 2>/dev/null)

    # Registry
    if curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:5002/v2/ | grep -q "200"; then
        log "✅ Registry: OK"
    else
        log "❌ Registry: FAILED"
        FAILED="${FAILED}Registry, "
    fi

    # MOMO App
    if curl -s -o /dev/null -w '%{http_code}' https://mo.wooo.work/health | grep -q "200"; then
        log "✅ MOMO App: OK"
    else
        log "❌ MOMO App: FAILED"
        FAILED="${FAILED}MOMO App, "
    fi

    # n8n (via K8s ClusterIP)
    if [ -n "$N8N_IP" ] && curl -s -o /dev/null -w '%{http_code}' "http://${N8N_IP}:5678/healthz" | grep -q "200"; then
        log "✅ n8n (K8s): OK"
    else
        log "❌ n8n (K8s): FAILED"
        FAILED="${FAILED}n8n, "
    fi

    # Superset (via K8s ClusterIP)
    if [ -n "$SUPERSET_IP" ] && curl -s -o /dev/null -w '%{http_code}' "http://${SUPERSET_IP}:8088/health" | grep -q "200"; then
        log "✅ Superset (K8s): OK"
    else
        log "❌ Superset (K8s): FAILED"
        FAILED="${FAILED}Superset, "
    fi

    # GitLab (可能需要更長時間)
    if curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8929/ | grep -q "200\|302"; then
        log "✅ GitLab: OK"
    else
        log "⚠️ GitLab: 啟動中... (需要幾分鐘)"
    fi

    # 結果通知
    log "=========================================="
    if [ -z "$FAILED" ]; then
        log "✅ 所有服務啟動成功"
        notify "✅ 系統啟動完成

所有服務狀態:
• Registry ✅
• MOMO App (K8s) ✅
• n8n (K8s) ✅
• Superset (K8s) ✅
• GitLab ✅

⏰ $(date '+%Y-%m-%d %H:%M:%S')"
    else
        log "⚠️ 部分服務異常: $FAILED"
        notify "⚠️ 系統啟動完成，但部分服務異常

異常服務: $FAILED

請檢查日誌: /var/log/momo_startup.log

⏰ $(date '+%Y-%m-%d %H:%M:%S')"
    fi
    log "=========================================="
}

# 執行
main "$@"
