#!/bin/bash

cliHandleRestoreCommands()
{
    local restore_type="$initial_command2"
    local action="$initial_command3"
    local name="$initial_command4"
    local extra="$initial_command5"
    local extra2="$initial_command6"

    if [[ -z "$restore_type" ]]; then
        cliShowRestoreHelp
        return
    fi

    case "$restore_type" in
        app)
            case "$action" in
                ""|help) cliShowRestoreHelp ;;
                start)
                    [[ -z "$name" ]] && { isNotice "No app name provided."; cliShowRestoreHelp; return; }
                    restoreAppStart "$name" "${extra:-latest}" "$extra2"
                    ;;
                list)
                    [[ -z "$name" ]] && { isNotice "No app name provided."; cliShowRestoreHelp; return; }
                    local idx
                    while IFS= read -r idx; do
                        [[ -z "$idx" ]] && continue
                        echo "--- $(resticLocationName "$idx") (loc $idx) ---"
                        engineSnapshotsJson "$idx" "$name"
                    done < <(resticEnabledLocations)
                    ;;
                *)
                    isNotice "Invalid app restore action: $action"
                    cliShowRestoreHelp
                    ;;
            esac
            ;;
        system)
            # Restore the latest system-config snapshot (settings + creds) into a
            # staging dir; never overwrites live config. Optional location idx.
            backupRestoreSystemConfig "$action"
            ;;
        migrate)
            case "$action" in
                app)
                    # restore migrate app <app_name> <source_host> [loc_idx] [opts...]
                    # opts: --no-pre-backup, --keep-urls, --json-progress
                    [[ -z "$name" ]]  && { isNotice "Usage: restore migrate app <app_name> <source_host> [loc_idx] [opts]"; return; }
                    [[ -z "$extra" ]] && { isNotice "Usage: restore migrate app <app_name> <source_host> [loc_idx] [opts]"; return; }
                    shift 4  # consume restore_type, action, name, extra
                    local app_idx="$1"; shift 2>/dev/null || true
                    migrateApplyApp "$extra" "$name" "$app_idx" "$@"
                    ;;
                system)
                    # restore migrate system <source_host> [loc_idx] [opts...]
                    [[ -z "$name" ]] && { isNotice "Usage: restore migrate system <source_host> [loc_idx] [opts]"; return; }
                    shift 3
                    migrateApplySystem "$name" "$@"
                    ;;
                discover)
                    # restore migrate discover [loc_idx]
                    migrateDiscoverHosts "$name"
                    ;;
                list)
                    # restore migrate list <source_host> [loc_idx]
                    [[ -z "$name" ]] && { isNotice "Usage: restore migrate list <source_host> [loc_idx]"; return; }
                    migrateDiscoverApps "$name" "$extra"
                    ;;
                preflight)
                    # restore migrate preflight <source_host> <app> [loc_idx]
                    # Emits a single JSON object describing the planned migrate.
                    [[ -z "$name" || -z "$extra" ]] && { isNotice "Usage: restore migrate preflight <source_host> <app> [loc_idx]"; return; }
                    migratePreflight "$name" "$extra" "$extra2"
                    ;;
                *)
                    isNotice "Invalid migrate action: $action"
                    cliShowRestoreHelp
                    ;;
            esac
            ;;
        first-run)
            case "$action" in
                discover)
                    [[ -z "$name" ]] && name="1"
                    restoreFirstRunDiscover "$name"
                    ;;
                bulk)
                    [[ -z "$name" || -z "$extra" ]] && { isNotice "Usage: restore first-run bulk <loc_idx> <source_host> <app1> [app2 ...]"; return; }
                    local loc_idx="$name"
                    local source_host="$extra"
                    shift 4
                    restoreFirstRunBulk "$loc_idx" "$source_host" "$@"
                    ;;
                *) cliShowRestoreHelp ;;
            esac
            ;;
        *)
            isNotice "Invalid restore type: $restore_type"
            cliShowRestoreHelp
            ;;
    esac
}
