#!/bin/bash

# Momentry 監控系統控制腳本
# 路徑: /Users/accusys/momentry_core_0.1/monitor/control/monitor_control.sh

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MONITOR_DIR="$(dirname "$SCRIPT_DIR")"
CONFIG_DIR="$MONITOR_DIR/config"
LOG_DIR="/Users/accusys/momentry/log/monitor"

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

# ============================================================
# 幫助信息
# ============================================================

show_help() {
    cat << EOF
Momentry 監控系統控制腳本

用法: $0 <command> [options]

命令:
    status                      查看監控狀態
    check <layer>               執行特定層級檢查
        layers: service, workflow, portal, database, users, storage, external, backup, node, python, all
    monitor                     持續監控 (每 5 分鐘)
    init                        初始化監控數據庫表
    logs <layer> [lines]       查看日誌
    clean                       清理歷史數據
    help                        顯示幫助

示例:
    $0 status                   查看所有監控狀態
    $0 check service           檢查服務狀態
    $0 check backup            檢查備份狀態
    $0 check all                執行全面檢查
    $0 logs anomaly 50          查看最近 50 條異常
    $0 init                     初始化數據庫表

EOF
}

# ============================================================
# 初始化
# ============================================================

init_monitor() {
    echo -e "${BLUE}初始化監控系統...${NC}"
    
    # 創建日誌目錄
    mkdir -p "$LOG_DIR"
    
    # 創建數據庫表
    echo "創建監控數據庫表..."
    psql -U accusys -h localhost -d momentry -f "$MONITOR_DIR/database/schema.sql" 2>/dev/null || \
        echo "數據庫表可能已存在"
    
    echo -e "${GREEN}初始化完成${NC}"
}

# ============================================================
# 狀態查看
# ============================================================

show_status() {
    echo ""
    echo "========================================"
    echo -e "${BLUE}Momentry 監控系統狀態${NC}"
    echo "========================================"
    echo ""
    
    # 服務狀態
    echo -e "${YELLOW}Layer 2: 服務狀態${NC}"
    local service_count=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(*) FROM monitor_services WHERE checked_at > NOW() - INTERVAL '5 minutes' AND status = 'up';" 2>/dev/null || echo "0")
    local service_total=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(DISTINCT service_name) FROM monitor_services;" 2>/dev/null || echo "0")
    echo "  服務: $service_count / $service_total 正常"
    echo ""
    
    # Workflow 狀態
    echo -e "${YELLOW}Layer 3: Workflow 狀態${NC}"
    local active_wf=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(*) FROM monitor_workflows WHERE is_active = true;" 2>/dev/null || echo "0")
    local idle_wf=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(*) FROM monitor_workflows WHERE idle_days > 30;" 2>/dev/null || echo "0")
    echo "  啟用 Workflow: $active_wf"
    echo "  閒置 Workflow (>30天): $idle_wf"
    echo ""
    
    # Database 狀態
    echo -e "${YELLOW}Layer 5: Database 狀態${NC}"
    local db_count=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(DISTINCT db_type) FROM monitor_databases WHERE checked_at > NOW() - INTERVAL '5 minutes';" 2>/dev/null || echo "0")
    echo "  監控資料庫: $db_count"
    echo ""
    
    # 異常狀態
    echo -e "${YELLOW}Layer 6: 異常狀態${NC}"
    local critical=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(*) FROM monitor_anomalies WHERE severity = 'critical' AND detected_at > NOW() - INTERVAL '24 hours';" 2>/dev/null || echo "0")
    local warning=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(*) FROM monitor_anomalies WHERE severity IN ('medium', 'high') AND detected_at > NOW() - INTERVAL '24 hours';" 2>/dev/null || echo "0")
    echo "  Critical: $critical"
    echo "  Warning: $warning"
    echo ""
    
    # Node.js 狀態
    echo -e "${YELLOW}Node.js 運行環境${NC}"
    local node_compliant=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(*) FROM node_version_baseline WHERE is_compliant = true;" 2>/dev/null || echo "0")
    local node_total=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(*) FROM node_version_baseline;" 2>/dev/null || echo "0")
    echo "  版本合規: $node_compliant / $node_total"
    echo ""
    
    # Python 狀態
    echo -e "${YELLOW}Python 運行環境${NC}"
    local python_compliant=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(*) FROM python_version_baseline WHERE is_compliant = true;" 2>/dev/null || echo "0")
    local python_total=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(*) FROM python_version_baseline;" 2>/dev/null || echo "0")
    echo "  版本合規: $python_compliant / $python_total"
    echo ""
    
    # 備份狀態
    echo -e "${YELLOW}Layer 7: 備份狀態${NC}"
    local total_backups=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(*) FROM backup_registry WHERE created_at > NOW() - INTERVAL '7 days';" 2>/dev/null || echo "0")
    local failed_backups=$(psql -U accusys -h localhost -d momentry -t -A -c "SELECT COUNT(*) FROM backup_registry WHERE status = 'failed' AND created_at > NOW() - INTERVAL '7 days';" 2>/dev/null || echo "0")
    echo "  本週備份: $total_backups"
    echo "  失敗: $failed_backups"
    echo ""
    
    echo "========================================"
    echo "使用 '$0 check <layer>' 執行檢查"
    echo ""
}

# ============================================================
# 執行檢查
# ============================================================

check_layer() {
    local layer=$1
    
    case $layer in
        service)
            echo -e "${BLUE}執行 Layer 2: 服務監控...${NC}"
            bash "$MONITOR_DIR/service/health_check.sh"
            ;;
        workflow)
            echo -e "${BLUE}執行 Layer 3: Workflow 監控...${NC}"
            bash "$MONITOR_DIR/workflow/n8n_workflow_monitor.sh"
            ;;
        portal)
            echo -e "${BLUE}執行 Layer 4: Portal 監控...${NC}"
            bash "$MONITOR_DIR/portal/page_monitor.sh"
            ;;
        database)
            echo -e "${BLUE}執行 Layer 5: Database 監控...${NC}"
            bash "$MONITOR_DIR/database/postgres_monitor.sh"
            bash "$MONITOR_DIR/database/redis_monitor.sh"
            bash "$MONITOR_DIR/database/qdrant_monitor.sh"
            ;;
        users)
            echo -e "${BLUE}執行 Layer 6: 使用者監控...${NC}"
            bash "$MONITOR_DIR/users/session_tracker.sh"
            ;;
        storage)
            echo -e "${BLUE}執行 Layer 7: Storage 監控...${NC}"
            bash "$MONITOR_DIR/storage/storage_manager.sh" status
            ;;
        backup)
            echo -e "${BLUE}執行 Layer 7: 備份監控...${NC}"
            bash "$MONITOR_DIR/storage/backup_monitor.sh" status
            ;;
        external)
            echo -e "${BLUE}執行 Layer 1: External 監控...${NC}"
            bash "$MONITOR_DIR/service/external_monitor.sh"
            ;;
        node)
            echo -e "${BLUE}執行 Node.js 版本監控...${NC}"
            bash "$MONITOR_DIR/service/node_monitor.sh"
            ;;
        python)
            echo -e "${BLUE}執行 Python 版本監控...${NC}"
            bash "$MONITOR_DIR/service/python_monitor.sh"
            ;;
        all)
            echo -e "${BLUE}執行全面監控檢查...${NC}"
            check_layer external
            check_layer service
            check_layer node
            check_layer python
            check_layer workflow
            check_layer portal
            check_layer database
            check_layer users
            echo -e "${GREEN}全面檢查完成${NC}"
            ;;
        *)
            echo -e "${RED}未知層級: $layer${NC}"
            show_help
            exit 1
            ;;
    esac
}

# ============================================================
# 持續監控
# ============================================================

run_monitor() {
    echo -e "${BLUE}開始持續監控 (每 5 分鐘)${NC}"
    echo "按 Ctrl+C 停止"
    echo ""
    
    while true; do
        local start_time=$(date +%s)
        
        check_layer all
        
        local end_time=$(date +%s)
        local elapsed=$((end_time - start_time))
        
        if [ $elapsed -lt 300 ]; then
            sleep $((300 - elapsed))
        fi
    done
}

# ============================================================
# 查看日誌
# ============================================================

show_logs() {
    local layer=${1:-anomaly}
    local lines=${2:-20}
    
    case $layer in
        anomaly)
            echo -e "${BLUE}最近異常記錄:${NC}"
            psql -U accusys -h localhost -d momentry -c "
                SELECT 
                    TO_CHAR(detected_at, 'YYYY-MM-DD HH24:MI:SS') as time,
                    severity,
                    anomaly_type,
                    username,
                    LEFT(description, 50) as desc
                FROM monitor_anomalies 
                ORDER BY detected_at DESC 
                LIMIT $lines;
            " 2>/dev/null || echo "無法連接資料庫"
            ;;
        service)
            echo -e "${BLUE}最近服務狀態:${NC}"
            psql -U accusys -h localhost -d momentry -c "
                SELECT 
                    service_name,
                    status,
                    response_time_ms,
                    TO_CHAR(checked_at, 'YYYY-MM-DD HH24:MI:SS') as time
                FROM monitor_services 
                ORDER BY checked_at DESC 
                LIMIT $lines;
            " 2>/dev/null || echo "無法連接資料庫"
            ;;
        workflow)
            echo -e "${BLUE}最近 Workflow 狀態:${NC}"
            psql -U accusys -h localhost -d momentry -c "
                SELECT 
                    workflow_name,
                    is_active,
                    idle_days,
                    suggestion,
                    TO_CHAR(checked_at, 'YYYY-MM-DD HH24:MI:SS') as time
                FROM monitor_workflows 
                ORDER BY checked_at DESC 
                LIMIT $lines;
            " 2>/dev/null || echo "無法連接資料庫"
            ;;
        *)
            echo -e "${RED}未知日誌類型: $layer${NC}"
            ;;
    esac
}

# ============================================================
# 清理歷史數據
# ============================================================

clean_history() {
    echo -e "${YELLOW}清理歷史數據...${NC}"
    
    psql -U accusys -h localhost -d momentry << EOF 2>/dev/null || true
-- 保留 30 天
DELETE FROM monitor_services WHERE checked_at < NOW() - INTERVAL '30 days';
DELETE FROM monitor_workflows WHERE checked_at < NOW() - INTERVAL '30 days';
DELETE FROM monitor_databases WHERE checked_at < NOW() - INTERVAL '30 days';
DELETE FROM monitor_external WHERE checked_at < NOW() - INTERVAL '30 days';
DELETE FROM monitor_portal_pages WHERE checked_at < NOW() - INTERVAL '30 days';

-- 保留 30 天版本基線
DELETE FROM node_version_baseline WHERE checked_at < NOW() - INTERVAL '30 days';
DELETE FROM node_process_tracking WHERE checked_at < NOW() - INTERVAL '30 days';
DELETE FROM python_version_baseline WHERE checked_at < NOW() - INTERVAL '30 days';
DELETE FROM python_script_tracking WHERE checked_at < NOW() - INTERVAL '30 days';

-- 保留 7 天會話
DELETE FROM monitor_sessions WHERE connected_at < NOW() - INTERVAL '7 days';

-- 保留 30 天登入
DELETE FROM monitor_logins WHERE login_at < NOW() - INTERVAL '30 days';

-- 保留 90 天異常
DELETE FROM monitor_anomalies WHERE detected_at < NOW() - INTERVAL '90 days';

-- 清理空閒空間
VACUUM ANALYZE;
EOF
    
    echo -e "${GREEN}清理完成${NC}"
}

# ============================================================
# 主程序
# ============================================================

main() {
    # 確保日誌目錄存在
    mkdir -p "$LOG_DIR"
    
    local command=${1:-status}
    
    case $command in
        status)
            show_status
            ;;
        check)
            check_layer ${2:-all}
            ;;
        monitor)
            run_monitor
            ;;
        init)
            init_monitor
            ;;
        logs)
            show_logs ${2:-anomaly} ${3:-20}
            ;;
        clean)
            clean_history
            ;;
        help|--help|-h)
            show_help
            ;;
        *)
            echo -e "${RED}未知命令: $command${NC}"
            show_help
            exit 1
            ;;
    esac
}

main "$@"
