#!/bin/bash

################################################################################
# Acronis Backup Status
################################################################################
# Purpose: Check status of backup operations using acrocmd
# Features:
# - Show active/running backups
# - Display recent backup history
# - Show backup task status
################################################################################

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
source "$SCRIPT_DIR/lib/common-functions.sh"
source "$SCRIPT_DIR/lib/system-detect.sh"

if [ "$EUID" -ne 0 ]; then
    print_error "This script must be run as root"
    exit 1
fi

clear
print_banner "Backup Status"

echo ""

# Check if acrocmd is available
if [ ! -f "/usr/sbin/acrocmd" ]; then
    print_error "acrocmd command-line tool not found"
    echo ""
    press_enter
    exit 1
fi

# Show active/running tasks
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo -e "${BOLD}Active Backup Tasks${NC}"
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""

task_output=$(/usr/sbin/acrocmd list tasks 2>&1)

if echo "$task_output" | grep -qi "no.*tasks\|error"; then
    echo -e "${GREEN}✓${NC} No active backup tasks running"
else
    echo "$task_output"
fi

echo ""

# Show recent activities
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo -e "${BOLD}Recent Backup Activities${NC}"
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""

activity_output=$(/usr/sbin/acrocmd list activities 2>&1)

if echo "$activity_output" | grep -qi "no.*activities\|error"; then
    echo -e "${YELLOW}No recent backup activities found${NC}"
    echo ""
    echo "This may indicate:"
    echo "  • No backups have been run yet"
    echo "  • Agent needs registration"
    echo "  • No backup plans configured"
else
    echo "$activity_output" | tail -20
fi

echo ""

# Parse logs for backup status
if [ -f "/var/lib/Acronis/BackupAndRecovery/MMS/mms.0.log" ]; then
    echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
    echo -e "${BOLD}Log Summary${NC}"
    echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
    echo ""

    # Count recent backup events
    log_file="/var/lib/Acronis/BackupAndRecovery/MMS/mms.0.log"

    completed=$(grep -ic "backup.*completed\|backup.*success" "$log_file" 2>/dev/null || echo "0")
    failed=$(grep -ic "backup.*failed\|backup.*error" "$log_file" 2>/dev/null || echo "0")
    started=$(grep -ic "backup.*started\|backup.*begin" "$log_file" 2>/dev/null || echo "0")

    echo "Backup Statistics (from current log):"
    echo "  • Started:   $started"
    echo "  • Completed: $completed"
    echo "  • Failed:    $failed"

    echo ""

    # Show last 5 backup-related events
    echo "Recent Events:"
    echo ""
    grep -i "backup" "$log_file" 2>/dev/null | tail -5 | while read -r line; do
        # Highlight status
        if echo "$line" | grep -qi "success\|completed"; then
            echo -e "  ${GREEN}✓${NC} $line"
        elif echo "$line" | grep -qi "fail\|error"; then
            echo -e "  ${RED}✗${NC} $line"
        else
            echo "  → $line"
        fi
    done
fi

echo ""
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""
echo -e "${BOLD}Options:${NC}"
echo ""
echo "  • View full logs: Select 'View Agent Logs' from menu"
echo "  • Trigger backup: Select 'Trigger Manual Backup'"
echo "  • Troubleshoot: Use 'Troubleshoot Backups' for diagnostics"
echo ""

press_enter
