#!/bin/bash

################################################################################
# Acronis Backup Manager
################################################################################
# Purpose: Main interface for Acronis backup operations
# Features:
# - List backups and archives
# - Trigger manual backups
# - View backup schedules
# - Monitor backup/recovery 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

# Check if Acronis is installed
if ! systemctl list-unit-files | grep -q "acronis_mms.service"; then
    print_error "Acronis is not installed"
    echo ""
    echo "Install Acronis first from the Acronis menu."
    echo ""
    press_enter
    exit 1
fi

# Check if acrocmd is available
if [ ! -f "/usr/sbin/acrocmd" ]; then
    print_error "acrocmd command-line tool not found"
    echo ""
    echo "This may indicate an incomplete Acronis installation."
    echo ""
    press_enter
    exit 1
fi

while true; do
    clear
    print_banner "Backup Management"

    echo ""
    echo -e "${BOLD}Agent Management${NC}"
    echo -e "  ${YELLOW}1)${NC} Check Agent Status"
    echo -e "  ${YELLOW}2)${NC} View Agent Logs"
    echo ""
    echo -e "${BOLD}Backup Operations${NC}"
    echo -e "  ${YELLOW}3)${NC} List Backups & Archives"
    echo -e "  ${YELLOW}4)${NC} Trigger Manual Backup"
    echo -e "  ${YELLOW}5)${NC} Check Backup Status"
    echo ""
    echo -e "${BOLD}Plan Management${NC}"
    echo -e "  ${YELLOW}6)${NC} View Backup Plans/Schedules"
    echo -e "  ${YELLOW}7)${NC} Manage Protection Plans"
    echo ""
    echo -e "${BOLD}Restore Operations${NC}"
    echo -e "  ${YELLOW}8)${NC} Restore from Backup (Future)"
    echo ""
    echo -e "  ${YELLOW}0)${NC} Return to Acronis Menu"
    echo ""
    echo -n "Select option: "
    read -r choice

    case "$choice" in
        1)
            bash "$SCRIPT_DIR/modules/backup/acronis-agent-status.sh"
            ;;
        2)
            bash "$SCRIPT_DIR/modules/backup/acronis-logs.sh"
            ;;
        3)
            bash "$SCRIPT_DIR/modules/backup/acronis-list-backups.sh"
            ;;
        4)
            bash "$SCRIPT_DIR/modules/backup/acronis-trigger-backup.sh"
            ;;
        5)
            bash "$SCRIPT_DIR/modules/backup/acronis-backup-status.sh"
            ;;
        6)
            bash "$SCRIPT_DIR/modules/backup/acronis-schedule-viewer.sh"
            ;;
        7)
            bash "$SCRIPT_DIR/modules/backup/acronis-plan-manager.sh"
            ;;
        8)
            bash "$SCRIPT_DIR/modules/backup/acronis-restore.sh"
            ;;
        0)
            exit 0
            ;;
        *)
            echo ""
            print_error "Invalid option"
            sleep 1
            ;;
    esac
done
