#!/bin/bash

################################################################################
# Acronis Schedule Viewer
################################################################################
# Purpose: View backup schedules and protection plans
# Features:
# - List all protection plans
# - Show backup schedules
# - Display plan details
################################################################################

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 Plans & Schedules"

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

# List protection plans
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo -e "${BOLD}Protection Plans${NC}"
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""

plan_output=$(/usr/sbin/acrocmd list plans 2>&1)

if echo "$plan_output" | grep -qi "error\|no.*plans"; then
    echo -e "${YELLOW}No protection plans found${NC}"
    echo ""
    echo "Protection plans define what, when, and how to back up."
    echo ""
    echo -e "${BOLD}To Create Protection Plans:${NC}"
    echo ""
    echo "1. Log in to Acronis web console"

    # Try to get cloud URL
    if [ -f "/etc/Acronis/Global.config" ]; then
        cloud_url=$(grep -oP 'CloudUrl[>=\"].*?https://[^\"<]+' /etc/Acronis/Global.config 2>/dev/null | grep -oP 'https://[^\"<]+' | head -1)
        if [ -n "$cloud_url" ]; then
            echo "   ${cloud_url}"
        fi
    fi

    echo ""
    echo "2. Navigate to: Devices → Select this server"
    echo "3. Click 'Add protection plan'"
    echo "4. Configure:"
    echo "   • Backup source (files/folders/volumes)"
    echo "   • Backup destination (cloud/local)"
    echo "   • Schedule (hourly/daily/weekly/monthly)"
    echo "   • Retention policy"
    echo "5. Save and activate plan"
else
    echo "$plan_output"
fi

echo ""
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""

# Check schedule status from service
echo -e "${BOLD}Schedule Service Status${NC}"
echo ""

if systemctl is-active --quiet acronis_schedule 2>/dev/null; then
    echo -e "${GREEN}✓${NC} Acronis scheduler is running"

    # Show recent schedule events from log
    if [ -f "/var/lib/Acronis/BackupAndRecovery/scheduler.log" ]; then
        echo ""
        echo "Recent scheduler activity:"
        echo ""
        tail -5 /var/lib/Acronis/BackupAndRecovery/scheduler.log 2>/dev/null | while read -r line; do
            echo "  $line"
        done
    fi
else
    echo -e "${YELLOW}⚠${NC} Acronis scheduler is not running"
    echo ""
    echo "Start it with: systemctl start acronis_schedule"
fi

echo ""
echo -e "${CYAN}════════════════════════════════════════════════════════════${NC}"
echo ""
echo -e "${BOLD}Next Steps:${NC}"
echo ""
echo "  • Trigger manual backup: Select 'Trigger Manual Backup'"
echo "  • Manage plans: Select 'Manage Protection Plans'"
echo "  • Check status: Select 'Check Backup Status'"
echo ""

press_enter
