#!/bin/bash
# Remote Deployment Script - Deploys supporting services via Proxmox API
# Usage: ./deploy-remote.sh [node-name]
# 
# This script can be run from any machine with Proxmox API access
# Requires: PROXMOX_HOST, PROXMOX_USER, PROXMOX_TOKEN environment variables
# OR: .env file with PROXMOX credentials

set -e

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

# Load environment variables
if [ -f "$SCRIPT_DIR/.env" ]; then
    set -a
    source "$SCRIPT_DIR/.env"
    set +a
fi

NODE="${1:-r630-01}"

# Proxmox API configuration
PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.11}"
PROXMOX_USER="${PROXMOX_USER:-root@pam}"
PROXMOX_TOKEN_NAME="${PROXMOX_TOKEN_NAME:-rpc-translator-deploy}"
PROXMOX_TOKEN_VALUE="${PROXMOX_TOKEN_VALUE}"

# Check if credentials are available
if [ -z "$PROXMOX_TOKEN_VALUE" ] && [ -z "$PROXMOX_PASSWORD" ]; then
    echo "❌ Error: Proxmox API credentials not found"
    echo ""
    echo "Please set one of:"
    echo "  - PROXMOX_TOKEN_VALUE (API token)"
    echo "  - PROXMOX_PASSWORD (password for $PROXMOX_USER)"
    echo ""
    echo "Or add to .env file:"
    echo "  PROXMOX_HOST=192.168.11.11"
    echo "  PROXMOX_USER=root@pam"
    echo "  PROXMOX_TOKEN_VALUE=your-token-here"
    echo ""
    exit 1
fi

echo "========================================="
echo "Remote Deployment - Supporting Services"
echo "========================================="
echo "Target Node: $NODE"
echo "Proxmox Host: $PROXMOX_HOST"
echo ""

# Function to call Proxmox API
proxmox_api() {
    local method=$1
    local endpoint=$2
    local data=$3
    
    local url="https://${PROXMOX_HOST}:8006/api2/json${endpoint}"
    local auth_header=""
    
    if [ -n "$PROXMOX_TOKEN_VALUE" ]; then
        auth_header="Authorization: PVEAPIToken=${PROXMOX_USER}!${PROXMOX_TOKEN_NAME}=${PROXMOX_TOKEN_VALUE}"
    else
        # Would need to get ticket first for password auth
        echo "⚠️  Password authentication not yet implemented in this script"
        echo "   Please use API token authentication"
        exit 1
    fi
    
    if [ -z "$data" ]; then
        curl -s -k -H "$auth_header" -X "$method" "$url"
    else
        curl -s -k -H "$auth_header" -H "Content-Type: application/json" -X "$method" -d "$data" "$url"
    fi
}

# Check if node is accessible
echo "Checking Proxmox connection..."
NODE_STATUS=$(proxmox_api GET "/nodes/$NODE/status" 2>&1)
if echo "$NODE_STATUS" | grep -q "401\|403\|authentication"; then
    echo "❌ Authentication failed"
    echo "Please check your PROXMOX credentials"
    exit 1
fi

if echo "$NODE_STATUS" | grep -q "404\|not found"; then
    echo "❌ Node '$NODE' not found"
    exit 1
fi

echo "✅ Connected to Proxmox"
echo ""

# Note: This script provides a framework but full container creation
# via API is complex. For now, it checks connectivity and provides instructions.
echo "⚠️  Note: Full container creation via API requires additional setup"
echo ""
echo "For automated deployment, use one of:"
echo ""
echo "1. Run on Proxmox host directly:"
echo "   ssh root@$PROXMOX_HOST"
echo "   cd /path/to/rpc-translator-138"
echo "   ./deploy-supporting-services.sh $NODE"
echo ""
echo "2. Use Proxmox Web UI:"
echo "   https://$PROXMOX_HOST:8006"
echo "   Create containers manually (see LXC_DEPLOYMENT.md)"
echo ""
echo "3. Use Proxmox MCP tools (if configured):"
echo "   Use the MCP Proxmox server tools to create containers"
echo ""
