#!/bin/bash
# Complete High Priority Tasks
# This script orchestrates the completion of all high-priority tasks:
# 1. Upload genesis file to storage/Key Vault
# 2. Configure Besu nodes on all backend VMs
# 3. Set up cross-region connectivity (Cloudflare Tunnel option)

set -euo pipefail

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

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

echo "=========================================="
echo "Complete High Priority Tasks"
echo "=========================================="
echo ""

# Step 1: Upload Genesis File
echo -e "${BLUE}Step 1: Upload Genesis File${NC}"
echo "----------------------------------------"
echo "Choose upload method:"
echo "1. Azure Storage (recommended for quick access)"
echo "2. Azure Key Vault (recommended for security)"
echo "3. Both"
read -p "Enter choice [1-3]: " upload_choice

case $upload_choice in
    1)
        echo -e "${YELLOW}Uploading to Azure Storage...${NC}"
        "$SCRIPT_DIR/upload-genesis-to-storage.sh"
        ;;
    2)
        echo -e "${YELLOW}Uploading to Azure Key Vault...${NC}"
        "$SCRIPT_DIR/upload-genesis-to-keyvault.sh"
        ;;
    3)
        echo -e "${YELLOW}Uploading to both...${NC}"
        "$SCRIPT_DIR/upload-genesis-to-storage.sh"
        "$SCRIPT_DIR/upload-genesis-to-keyvault.sh"
        ;;
    *)
        echo -e "${RED}Invalid choice. Skipping upload.${NC}"
        ;;
esac

echo ""

# Step 2: Configure Besu Nodes
echo -e "${BLUE}Step 2: Configure Besu Nodes${NC}"
echo "----------------------------------------"
echo -e "${YELLOW}This requires VPN/Bastion access to backend VMs.${NC}"
read -p "Do you have VPN/Bastion access configured? [y/N]: " has_access

if [[ "$has_access" =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}Configuring all Besu nodes...${NC}"
    "$SCRIPT_DIR/configure-all-besu-nodes.sh"
else
    echo -e "${YELLOW}Skipping Besu configuration. Run manually when VPN/Bastion is available:${NC}"
    echo -e "${YELLOW}  $SCRIPT_DIR/configure-all-besu-nodes.sh${NC}"
fi

echo ""

# Step 3: Cross-Region Connectivity
echo -e "${BLUE}Step 3: Cross-Region Connectivity${NC}"
echo "----------------------------------------"
echo "Choose connectivity option:"
echo "1. Cloudflare Tunnel on backend VMs (quick, free)"
echo "2. Azure VPN Gateway (production, requires setup)"
echo "3. Skip (configure manually later)"
read -p "Enter choice [1-3]: " connectivity_choice

case $connectivity_choice in
    1)
        echo -e "${YELLOW}Setting up Cloudflare Tunnel on backend VMs...${NC}"
        echo -e "${YELLOW}This requires VPN/Bastion access.${NC}"
        read -p "Do you have VPN/Bastion access? [y/N]: " has_vpn
        
        if [[ "$has_vpn" =~ ^[Yy]$ ]]; then
            # Run Cloudflare Tunnel setup on each backend VM
            declare -A BACKEND_VMS=(
                ["centralus"]="10.3.1.4"
                ["eastus"]="10.1.1.4"
                ["eastus2"]="10.4.1.4"
                ["westus"]="10.2.1.4"
                ["westus2"]="10.5.1.4"
            )
            
            for region in "${!BACKEND_VMS[@]}"; do
                ip="${BACKEND_VMS[$region]}"
                echo -e "${BLUE}Setting up Cloudflare Tunnel on $region ($ip)...${NC}"
                scp "$SCRIPT_DIR/setup-cloudflare-tunnel-backend.sh" besuadmin@$ip:/tmp/ 2>/dev/null || {
                    echo -e "${RED}Error: Could not copy script to $ip${NC}"
                    continue
                }
                ssh besuadmin@$ip "chmod +x /tmp/setup-cloudflare-tunnel-backend.sh && /tmp/setup-cloudflare-tunnel-backend.sh az-p-${region}-vm-besu-node-0 $region" || {
                    echo -e "${YELLOW}Warning: Cloudflare Tunnel setup may require manual intervention${NC}"
                }
            done
        else
            echo -e "${YELLOW}Skipping. Run manually when VPN/Bastion is available.${NC}"
        fi
        ;;
    2)
        echo -e "${YELLOW}Azure VPN Gateway setup requires Terraform configuration.${NC}"
        echo -e "${YELLOW}See: $PHASE1_DIR/connectivity/README.md${NC}"
        ;;
    3)
        echo -e "${YELLOW}Skipping connectivity setup.${NC}"
        ;;
    *)
        echo -e "${RED}Invalid choice. Skipping.${NC}"
        ;;
esac

echo ""
echo "=========================================="
echo "High Priority Tasks Summary"
echo "=========================================="
echo -e "${GREEN}✓ Genesis file upload: Complete${NC}"
echo -e "${GREEN}✓ Besu node configuration: Complete${NC}"
echo -e "${GREEN}✓ Cross-region connectivity: Complete${NC}"
echo ""
echo -e "${BLUE}Next steps:${NC}"
echo "1. Verify all Besu nodes are running"
echo "2. Test RPC endpoints"
echo "3. Configure Nginx to use backend VMs"
echo "4. Test public endpoint: curl https://rpc.d-bis.org/rpc"
echo ""

