#!/usr/bin/env bash
# Copy flush transaction scripts to Proxmox host
# Usage: ./scripts/copy-flush-scripts-to-proxmox.sh [host] [user]

set -euo pipefail

# Suppress locale warnings
export LC_ALL=C
export LANG=C

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

PROXMOX_HOST="${1:-192.168.11.10}"
PROXMOX_USER="${2:-root}"
REMOTE_BASE_DIR="${REMOTE_BASE_DIR:-/home/intlc/projects/proxmox}"

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

echo "========================================="
echo "Copy Flush Scripts to Proxmox Host"
echo "========================================="
echo ""
echo "Proxmox Host: $PROXMOX_HOST"
echo "User: $PROXMOX_USER"
echo "Remote Directory: $REMOTE_BASE_DIR"
echo ""

# Check if SSH key is available
log_info "Testing SSH connection..."
if ! ssh -o BatchMode=yes -o ConnectTimeout=5 "$PROXMOX_USER@$PROXMOX_HOST" exit 2>/dev/null; then
    log_error "SSH connection to $PROXMOX_HOST failed"
    echo ""
    echo "Please ensure:"
    echo "  1. SSH key is set up for passwordless access"
    echo "  2. Host is reachable: ping $PROXMOX_HOST"
    echo "  3. User has access: ssh $PROXMOX_USER@$PROXMOX_HOST"
    exit 1
fi

log_success "SSH connection successful"
echo ""

# Create remote directory structure
log_info "Creating remote directory structure..."
ssh "$PROXMOX_USER@$PROXMOX_HOST" "mkdir -p $REMOTE_BASE_DIR/scripts" || {
    log_error "Failed to create remote directories"
    exit 1
}

log_success "Remote directories ready"
echo ""

# List of scripts to copy
FLUSH_SCRIPTS=(
    "flush-all-mempools-proxmox.sh"
    "flush-all-stuck-transactions.sh"
    "clear-transaction-pool-database.sh"
)

# Copy scripts
log_info "Copying flush scripts..."
COPIED=0
FAILED=0

for script in "${FLUSH_SCRIPTS[@]}"; do
    SCRIPT_PATH="$PROJECT_ROOT/scripts/$script"
    
    if [ ! -f "$SCRIPT_PATH" ]; then
        log_warn "Script not found: $script"
        ((FAILED++))
        continue
    fi
    
    log_info "Copying $script..."
    if scp "$SCRIPT_PATH" "$PROXMOX_USER@$PROXMOX_HOST:$REMOTE_BASE_DIR/scripts/" 2>/dev/null; then
        # Make script executable on remote host
        ssh "$PROXMOX_USER@$PROXMOX_HOST" "chmod +x $REMOTE_BASE_DIR/scripts/$script" 2>/dev/null
        log_success "✓ Copied: $script"
        ((COPIED++))
    else
        log_error "✗ Failed to copy: $script"
        ((FAILED++))
    fi
done

echo ""
echo "========================================="
echo "Summary"
echo "========================================="
echo ""
log_success "Successfully copied: $COPIED/${#FLUSH_SCRIPTS[@]} scripts"

if [ $FAILED -gt 0 ]; then
    log_warn "Failed to copy: $FAILED scripts"
fi

echo ""
log_info "Scripts are now available on Proxmox host at:"
log_info "  $REMOTE_BASE_DIR/scripts/"
echo ""
log_info "To run on Proxmox host:"
log_info "  ssh $PROXMOX_USER@$PROXMOX_HOST"
log_info "  cd $REMOTE_BASE_DIR"
log_info "  ./scripts/flush-all-mempools-proxmox.sh"
echo ""

