#!/usr/bin/env bash
# Copy Deployment Scripts to Proxmox Host
# Copies all necessary deployment scripts and configuration to the Proxmox host

set -euo pipefail

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

PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
PROXMOX_USER="${PROXMOX_USER:-root}"
REMOTE_BASE_DIR="${REMOTE_BASE_DIR:-/opt/smom-dbis-138-proxmox}"

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

echo "========================================="
echo "Copy Deployment 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
if ! ssh -o BatchMode=yes -o ConnectTimeout=5 "$PROXMOX_USER@$PROXMOX_HOST" exit 2>/dev/null; then
    echo "❌ 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

echo "✅ SSH connection successful"
echo ""

# Create remote directory structure
echo "Creating remote directory structure..."
ssh "$PROXMOX_USER@$PROXMOX_HOST" "mkdir -p $REMOTE_BASE_DIR/{scripts/{deployment,validation,network,manage},config,lib,install,logs,docs}" || {
    echo "❌ Failed to create remote directories"
    exit 1
}

echo "✅ Remote directories created"
echo ""

# Copy configuration files
echo "Copying configuration files..."
scp -r "$PROJECT_ROOT/smom-dbis-138-proxmox/config/"* "$PROXMOX_USER@$PROXMOX_HOST:$REMOTE_BASE_DIR/config/" 2>/dev/null || {
    echo "⚠ Configuration files copy had issues (may not exist)"
}

echo "✅ Configuration files copied"
echo ""

# Copy library files
echo "Copying library files..."
if [[ -d "$PROJECT_ROOT/smom-dbis-138-proxmox/lib" ]]; then
    scp -r "$PROJECT_ROOT/smom-dbis-138-proxmox/lib/"* "$PROXMOX_USER@$PROXMOX_HOST:$REMOTE_BASE_DIR/lib/" || {
        echo "⚠ Library files copy had issues"
    }
    echo "✅ Library files copied"
else
    echo "⚠ lib/ directory not found, skipping"
fi
echo ""

# Copy deployment scripts
echo "Copying deployment scripts..."
if [[ -d "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/deployment" ]]; then
    scp "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/deployment/"*.sh "$PROXMOX_USER@$PROXMOX_HOST:$REMOTE_BASE_DIR/scripts/deployment/" || {
        echo "❌ Failed to copy deployment scripts"
        exit 1
    }
    # Make scripts executable
    ssh "$PROXMOX_USER@$PROXMOX_HOST" "chmod +x $REMOTE_BASE_DIR/scripts/deployment/*.sh"
    echo "✅ Deployment scripts copied and made executable"
else
    echo "❌ scripts/deployment/ directory not found"
    exit 1
fi
echo ""

# Copy validation scripts
echo "Copying validation scripts..."
if [[ -d "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/validation" ]]; then
    scp "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/validation/"*.sh "$PROXMOX_USER@$PROXMOX_HOST:$REMOTE_BASE_DIR/scripts/validation/" 2>/dev/null || {
        echo "⚠ Validation scripts copy had issues"
    }
    ssh "$PROXMOX_USER@$PROXMOX_HOST" "chmod +x $REMOTE_BASE_DIR/scripts/validation/*.sh 2>/dev/null || true"
    echo "✅ Validation scripts copied"
else
    echo "⚠ scripts/validation/ directory not found"
fi
echo ""

# Copy network scripts
echo "Copying network scripts..."
if [[ -d "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/network" ]]; then
    scp "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/network/"*.sh "$PROXMOX_USER@$PROXMOX_HOST:$REMOTE_BASE_DIR/scripts/network/" 2>/dev/null || true
    ssh "$PROXMOX_USER@$PROXMOX_HOST" "chmod +x $REMOTE_BASE_DIR/scripts/network/*.sh 2>/dev/null || true"
    echo "✅ Network scripts copied"
fi
echo ""

# Copy install scripts (if needed)
echo "Copying install scripts..."
if [[ -d "$PROJECT_ROOT/smom-dbis-138-proxmox/install" ]]; then
    scp "$PROJECT_ROOT/smom-dbis-138-proxmox/install/"*.sh "$PROXMOX_USER@$PROXMOX_HOST:$REMOTE_BASE_DIR/install/" 2>/dev/null || {
        echo "⚠ Install scripts copy had issues"
    }
    ssh "$PROXMOX_USER@$PROXMOX_HOST" "chmod +x $REMOTE_BASE_DIR/install/*.sh 2>/dev/null || true"
    echo "✅ Install scripts copied"
fi
echo ""

# Verify copied files
echo "Verifying copied files..."
echo "Deployment scripts on remote host:"
ssh "$PROXMOX_USER@$PROXMOX_HOST" "ls -lh $REMOTE_BASE_DIR/scripts/deployment/*.sh 2>/dev/null | head -10" || echo "  (none found)"

echo ""
echo "========================================="
echo "✅ Files Copied Successfully"
echo "========================================="
echo ""
echo "Remote location: $REMOTE_BASE_DIR"
echo ""
echo "You can now SSH to the Proxmox host and run:"
echo "  ssh $PROXMOX_USER@$PROXMOX_HOST"
echo "  cd $REMOTE_BASE_DIR"
echo "  sudo ./scripts/deployment/deploy-phased.sh --source-project /path/to/smom-dbis-138"
echo ""
echo "Or run remotely:"
echo "  ssh $PROXMOX_USER@$PROXMOX_HOST 'cd $REMOTE_BASE_DIR && sudo ./scripts/deployment/deploy-phased.sh --source-project /path/to/smom-dbis-138'"
echo ""

