#!/bin/bash

# Momentry 服務故障排除快速參考
# 用法: ./troubleshoot.sh [service]
# 例如: ./troubleshoot.sh n8n

SERVICE=${1:-all}

echo "========================================"
echo "Momentry 故障排除快速檢查"
echo "========================================"
echo ""

case "$SERVICE" in
  all)
    echo "執行所有服務健康檢查..."
    /Users/accusys/momentry_core_0.1/monitor/service/health_check.sh
    ;;
    
  postgresql|postgres|pg)
    echo "=== PostgreSQL 診斷 ==="
    echo "狀態: $(pg_isready -h localhost -p 5432 -U accusys 2>&1)"
    echo "連線測試: $(psql -U accusys -h localhost -d momentry -c 'SELECT 1' 2>&1 | head -1)"
    echo "數據庫列表:"
    psql -U accusys -h localhost -l 2>/dev/null | grep -v "rows)" || echo "  無法連線"
    echo ""
    echo "日誌: tail -50 /Users/accusys/momentry/log/postgresql.log"
    ;;
    
  redis)
    echo "=== Redis 診斷 ==="
    echo "狀態: $(redis-cli -a accusys ping 2>&1)"
    echo "記憶體: $(redis-cli -a accusys INFO memory 2>/dev/null | grep used_memory_human | head -1)"
    echo "連線數: $(redis-cli -a accusys INFO clients 2>/dev/null | grep connected_clients | head -1)"
    echo "Keys: $(redis-cli -a accusys DBSIZE 2>/dev/null)"
    echo ""
    echo "日誌: tail -50 /Users/accusys/momentry/log/redis.log"
    ;;
    
  mariadb|mysql)
    echo "=== MariaDB 診斷 ==="
    echo "狀態: $(mysql -u accusys -e 'SELECT 1' 2>&1 | head -1)"
    echo "用戶:"
    mysql -u accusys -e "SELECT user, host FROM mysql.user;" 2>/dev/null || echo "  無法連線"
    echo ""
    echo "日誌: tail -50 /Users/accusys/momentry/log/mariadb.log"
    ;;
    
  n8n)
    echo "=== n8n 診斷 ==="
    echo "Web 訪問: $(curl -s -o /dev/null -w '%{http_code}' http://localhost:8085/ 2>&1)"
    echo "Port 8085: $(lsof -i :8085 | tail -1)"
    echo ""
    echo "PostgreSQL 連線:"
    psql -U n8n -h localhost -d n8n -c "SELECT COUNT(*) as workflows FROM workflow_entity;" 2>&1
    psql -U n8n -h localhost -d n8n -c "SELECT email, \"firstName\", \"roleSlug\" FROM \"user\";" 2>&1
    echo ""
    echo "Redis Queue:"
    redis-cli -a accusys LLEN bull:n8n:wait 2>/dev/null || echo "  無法連線"
    echo ""
    echo "日誌: tail -50 /Users/accusys/momentry/log/n8n-main.log"
    ;;
    
  ollama)
    echo "=== Ollama 診斷 ==="
    echo "API 狀態: $(curl -s http://localhost:11434/api/tags 2>&1 | head -1)"
    echo "模型列表:"
    curl -s http://localhost:11434/api/tags 2>/dev/null | jq -r '.models[].name' 2>/dev/null || echo "  無法獲取"
    echo ""
    echo "日誌: tail -50 /Users/accusys/momentry/log/ollama.log"
    ;;
    
  qdrant)
    echo "=== Qdrant 診斷 ==="
    echo "API 狀態: $(curl -s -o /dev/null -w '%{http_code}' -H 'api-key: Test3200Test3200' http://localhost:6333/ 2>&1)"
    echo "Collections:"
    curl -s -H "api-key: Test3200Test3200" http://localhost:6333/collections 2>/dev/null | jq -r '.result[].name' 2>/dev/null || echo "  無法獲取"
    echo ""
    echo "日誌: tail -50 /Users/accusys/momentry/log/qdrant.log"
    ;;
    
  caddy)
    echo "=== Caddy 診斷 ==="
    echo "Admin API: $(curl -s http://localhost:2019/config/ 2>&1 | head -1)"
    echo "Port 2019: $(lsof -i :2019 | tail -1)"
    echo "Port 443: $(lsof -i :443 | tail -1)"
    echo ""
    echo "配置: cat /Users/accusys/momentry/etc/Caddyfile"
    echo "日誌: tail -50 /Users/accusys/momentry/log/caddy.log"
    ;;
    
  gitea)
    echo "=== Gitea 診斷 ==="
    echo "Web: $(curl -s -o /dev/null -w '%{http_code}' http://localhost:3000/ 2>&1)"
    echo "Port 3000: $(lsof -i :3000 | tail -1)"
    echo ""
    echo "日誌: tail -50 /Users/accusys/momentry/log/gitea.log"
    ;;
    
  sftpgo)
    echo "=== SFTPGo 診斷 ==="
    echo "HTTP: $(curl -s -o /dev/null -w '%{http_code}' http://localhost:8080/ 2>&1)"
    echo "SFTP Port 2022: $(lsof -i :2022 | tail -1)"
    echo ""
    echo "日誌: tail -50 /Users/accusys/momentry/log/sftpgo.log"
    ;;
    
  mongodb|mongo)
    echo "=== MongoDB 診斷 ==="
    mongosh --quiet --username accusys --password Test3200Test3200 --authenticationDatabase admin --eval "db.adminCommand('ping')" 2>&1
    echo "數據庫:"
    mongosh --quiet --username accusys --password Test3200Test3200 --authenticationDatabase admin --eval "db.adminCommand({listDatabases:1})" 2>&1 | grep name || echo "  無法獲取"
    echo ""
    echo "日誌: tail -50 /Users/accusys/momentry/log/mongodb.log"
    ;;
    
  php)
    echo "=== PHP-FPM 診斷 ==="
    echo "進程: $(pgrep -a php-fpm | head -1)"
    echo "Port 9000: $(lsof -i :9000 | tail -1)"
    echo ""
    echo "日誌: tail -50 /Users/accusys/momentry/log/php.error.log"
    ;;
    
  rustdesk)
    echo "=== RustDesk 診斷 ==="
    echo "hbbs: $(pgrep -a hbbs | head -1)"
    echo "hbbr: $(pgrep -a hbbr | head -1)"
    echo "Port 21116: $(lsof -i :21116 | tail -1)"
    echo "Port 21117: $(lsof -i :21117 | tail -1)"
    ;;
    
  *)
    echo "用法: $0 [service]"
    echo ""
    echo "可用服務:"
    echo "  all         - 執行所有健康檢查"
    echo "  postgresql  - PostgreSQL 診斷"
    echo "  redis       - Redis 診斷"
    echo "  mariadb     - MariaDB 診斷"
    echo "  n8n         - n8n 診斷"
    echo "  ollama      - Ollama 診斷"
    echo "  qdrant      - Qdrant 診斷"
    echo "  caddy       - Caddy 診斷"
    echo "  gitea       - Gitea 診斷"
    echo "  sftpgo      - SFTPGo 診斷"
    echo "  mongodb     - MongoDB 診斷"
    echo "  php         - PHP-FPM 診斷"
    echo "  rustdesk    - RustDesk 診斷"
    ;;
esac
