#!/bin/bash
# =============================================================================
# WOOO AIOps - 全服務備份總控腳本
# 版本: 3.0.0
# 建立日期: 2026-03-12
# 2026-04-05 Claude Code: 加入 AWOOOI DB (v1→v2) — 首席架構師備份審計
# 2026-04-05 Claude Code: 加入 Langfuse/Monitoring/SignOz/Open-WebUI/ClawBot (v2→v3) — 備份覆蓋率審計
# =============================================================================

set -euo pipefail

source "$(dirname "$0")/common.sh"

main() {
    local start_time=$(date +%s)
    local failed=0
    local total=9

    log_info "╔══════════════════════════════════════════════════════════════╗"
    log_info "║          WOOO AIOps - 全服務備份開始 (v3.0)                  ║"
    log_info "╚══════════════════════════════════════════════════════════════╝"

    # 備份 Gitea
    log_info ">>> [1/${total}] 備份 Gitea..."
    if /backup/scripts/backup-gitea.sh; then
        log_success "    Gitea 備份成功"
    else
        log_error "    Gitea 備份失敗"
        failed=$((failed+1))
    fi

    # 備份 MOMO Pro
    log_info ">>> [2/${total}] 備份 MOMO Pro..."
    if /backup/scripts/backup-momo.sh; then
        log_success "    MOMO Pro 備份成功"
    else
        log_error "    MOMO Pro 備份失敗"
        failed=$((failed+1))
    fi

    # 備份 Harbor
    log_info ">>> [3/${total}] 備份 Harbor..."
    if /backup/scripts/backup-harbor.sh; then
        log_success "    Harbor 備份成功"
    else
        log_error "    Harbor 備份失敗"
        failed=$((failed+1))
    fi

    # 備份 AWOOOI DB (awoooi_prod + k3s_datastore)
    # 2026-04-05 Claude Code: 首席架構師備份審計後加入
    log_info ">>> [4/${total}] 備份 AWOOOI DB..."
    if /backup/scripts/backup-awoooi.sh; then
        log_success "    AWOOOI DB 備份成功"
    else
        log_error "    AWOOOI DB 備份失敗"
        failed=$((failed+1))
    fi

    # 備份 Langfuse (AI 追蹤/評測數據)
    # 2026-04-05 Claude Code: 備份覆蓋率審計後加入
    log_info ">>> [5/${total}] 備份 Langfuse..."
    if /backup/scripts/backup-langfuse.sh; then
        log_success "    Langfuse 備份成功"
    else
        log_error "    Langfuse 備份失敗"
        failed=$((failed+1))
    fi

    # 備份 Monitoring (Prometheus + Grafana + Alertmanager)
    # 2026-04-05 Claude Code: 備份覆蓋率審計後加入
    log_info ">>> [6/${total}] 備份 Monitoring..."
    if /backup/scripts/backup-monitoring.sh; then
        log_success "    Monitoring 備份成功"
    else
        log_error "    Monitoring 備份失敗"
        failed=$((failed+1))
    fi

    # 備份 SignOz (ClickHouse + SQLite)
    # 2026-04-05 Claude Code: 備份覆蓋率審計後加入
    log_info ">>> [7/${total}] 備份 SignOz..."
    if /backup/scripts/backup-signoz.sh; then
        log_success "    SignOz 備份成功"
    else
        log_error "    SignOz 備份失敗"
        failed=$((failed+1))
    fi

    # 備份 Open-WebUI (LLM 對話紀錄，從 188)
    # 2026-04-05 Claude Code: 備份覆蓋率審計後加入
    log_info ">>> [8/${total}] 備份 Open-WebUI (188)..."
    if /backup/scripts/backup-open-webui.sh; then
        log_success "    Open-WebUI 備份成功"
    else
        log_error "    Open-WebUI 備份失敗"
        failed=$((failed+1))
    fi

    # 備份 ClawBot Redis (狀態/快取，從 188，低優先)
    # 2026-04-05 Claude Code: 備份覆蓋率審計後加入
    log_info ">>> [9/${total}] 備份 ClawBot Redis (188)..."
    if /backup/scripts/backup-clawbot.sh; then
        log_success "    ClawBot Redis 備份成功"
    else
        log_error "    ClawBot Redis 備份失敗"
        failed=$((failed+1))
    fi

    local end_time=$(date +%s)
    local duration=$((end_time - start_time))

    log_info "╔══════════════════════════════════════════════════════════════╗"
    if [ $failed -eq 0 ]; then
        log_success "║   全服務備份完成 (${duration}s) - 全部成功 (${total}/${total})       ║"
        notify_clawbot "success" "all" "全服務備份完成 (${total}/${total} 成功)" "${duration}"
    else
        log_error "║   全服務備份完成 (${duration}s) - ${failed} 個失敗 ($((total-failed))/${total}) ║"
        notify_clawbot "warning" "all" "全服務備份完成 ($((total-failed))/${total} 成功)" "${duration}"
    fi
    log_info "╚══════════════════════════════════════════════════════════════╝"

    return $failed
}

main "$@"
