#!/usr/bin/env bash
# Sync verified working files to ml110 (192.168.11.10)
# This script deletes old files and copies only verified working files

set -euo pipefail

REMOTE_HOST="192.168.11.10"
REMOTE_USER="root"
REMOTE_BASE="/opt"
LOCAL_PROJECT_ROOT="/home/intlc/projects/proxmox"
SOURCE_PROJECT="$LOCAL_PROJECT_ROOT/smom-dbis-138-proxmox"
SOURCE_SMOM="/home/intlc/projects/smom-dbis-138"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

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

# Check if sshpass is available
if ! command -v sshpass &> /dev/null; then
    log_error "sshpass is required. Install with: apt-get install sshpass"
    exit 1
fi

# Check if rsync is available
if ! command -v rsync &> /dev/null; then
    log_error "rsync is required. Install with: apt-get install rsync"
    exit 1
fi

log_info "=== Syncing Verified Working Files to ml110 ==="
log_info "Remote: ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_BASE}"
log_info ""

# Test SSH connection
log_info "Testing SSH connection..."
if ! sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 \
    "${REMOTE_USER}@${REMOTE_HOST}" "echo 'Connection successful'" 2>/dev/null; then
    log_error "Cannot connect to ${REMOTE_HOST}. Check network and credentials."
    exit 1
fi
log_info "✓ SSH connection successful"
echo ""

# Step 1: Backup existing files (optional but recommended)
log_info "=== Step 1: Creating backup of existing files ==="
BACKUP_DIR="/opt/backup-$(date +%Y%m%d-%H%M%S)"
sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no "${REMOTE_USER}@${REMOTE_HOST}" \
    "mkdir -p ${BACKUP_DIR} && \
     (test -d ${REMOTE_BASE}/smom-dbis-138 && cp -r ${REMOTE_BASE}/smom-dbis-138 ${BACKUP_DIR}/ || true) && \
     (test -d ${REMOTE_BASE}/smom-dbis-138-proxmox && cp -r ${REMOTE_BASE}/smom-dbis-138-proxmox ${BACKUP_DIR}/ || true) && \
     echo 'Backup created at ${BACKUP_DIR}'"
log_info "✓ Backup created at ${BACKUP_DIR}"
echo ""

# Step 2: Delete old files
log_info "=== Step 2: Removing old files ==="
log_warn "This will delete all existing files in /opt/smom-dbis-138 and /opt/smom-dbis-138-proxmox"
# Auto-confirm for non-interactive execution
log_info "Auto-confirming deletion (non-interactive mode)"

sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no "${REMOTE_USER}@${REMOTE_HOST}" \
    "rm -rf ${REMOTE_BASE}/smom-dbis-138-proxmox && \
     echo '✓ Removed old smom-dbis-138-proxmox directory'"
log_info "✓ Old files removed"
echo ""

# Step 3: Copy verified working files
log_info "=== Step 3: Copying verified working files ==="

# Copy smom-dbis-138-proxmox (entire directory - all files are verified)
log_info "Copying smom-dbis-138-proxmox directory..."
rsync -avz --delete \
    --exclude='.git' \
    --exclude='*.log' \
    --exclude='logs/*' \
    --exclude='node_modules' \
    --exclude='__pycache__' \
    -e "sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no" \
    "${SOURCE_PROJECT}/" \
    "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_BASE}/smom-dbis-138-proxmox/"

log_info "✓ smom-dbis-138-proxmox copied"
echo ""

# Copy smom-dbis-138 (only config and keys, preserve existing keys)
log_info "Copying smom-dbis-138 (config and keys only)..."
sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no "${REMOTE_USER}@${REMOTE_HOST}" \
    "mkdir -p ${REMOTE_BASE}/smom-dbis-138/config ${REMOTE_BASE}/smom-dbis-138/keys/validators ${REMOTE_BASE}/smom-dbis-138/keys/oracle"

# Copy config files
rsync -avz \
    -e "sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no" \
    "${SOURCE_SMOM}/config/" \
    "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_BASE}/smom-dbis-138/config/"

# Copy keys (preserve existing validator keys, but update if newer)
rsync -avz \
    -e "sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no" \
    "${SOURCE_SMOM}/keys/" \
    "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_BASE}/smom-dbis-138/keys/"

log_info "✓ smom-dbis-138 config and keys copied"
echo ""

# Step 4: Set permissions
log_info "=== Step 4: Setting permissions ==="
sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no "${REMOTE_USER}@${REMOTE_HOST}" \
    "chmod +x ${REMOTE_BASE}/smom-dbis-138-proxmox/scripts/*.sh && \
     chmod +x ${REMOTE_BASE}/smom-dbis-138-proxmox/scripts/*/*.sh && \
     chmod +x ${REMOTE_BASE}/smom-dbis-138-proxmox/install/*.sh && \
     chmod 600 ${REMOTE_BASE}/smom-dbis-138/keys/**/*.priv ${REMOTE_BASE}/smom-dbis-138/keys/**/*.pem 2>/dev/null || true && \
     echo '✓ Permissions set'"
log_info "✓ Permissions configured"
echo ""

# Step 5: Verify copied files
log_info "=== Step 5: Verifying copied files ==="
sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no "${REMOTE_USER}@${REMOTE_HOST}" \
    "echo 'smom-dbis-138-proxmox:' && \
     ls -la ${REMOTE_BASE}/smom-dbis-138-proxmox/ | head -10 && \
     echo '' && \
     echo 'smom-dbis-138:' && \
     ls -la ${REMOTE_BASE}/smom-dbis-138/ | head -10 && \
     echo '' && \
     echo 'Validator keys:' && \
     ls -d ${REMOTE_BASE}/smom-dbis-138/keys/validators/*/ 2>/dev/null | wc -l && \
     echo 'validator directories found'"

log_info "✓ Verification complete"
echo ""

log_info "=== Sync Complete ==="
log_info "Files synced to: ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_BASE}"
log_info "Backup location: ${BACKUP_DIR}"
log_info ""
log_info "Next steps:"
log_info "  1. SSH to ml110: ssh ${REMOTE_USER}@${REMOTE_HOST}"
log_info "  2. Verify files: cd ${REMOTE_BASE}/smom-dbis-138-proxmox && ls -la"
log_info "  3. Check config: cat config/proxmox.conf"
log_info "  4. Run deployment: ./deploy-all.sh"

