#!/usr/bin/env bash
# Deploy All Infrastructure Phases

set -e

cd "$(dirname "$0")/../.."

# Color codes

echo "==================================================================="
echo "  CHAIN-138 INFRASTRUCTURE DEPLOYMENT - ALL PHASES"
echo "==================================================================="

# Load environment variables
if [ -f .env ]; then
    source .env 2>/dev/null || true
fi

PHASES=(
    "Phase 1: Azure Infrastructure"
    "Phase 2: Kubernetes Resources"
    "Phase 3: Besu Network"
    "Phase 4: Monitoring and Explorer"
)

echo "This will deploy Chain-138 infrastructure in 4 phases:"
for phase in "${PHASES[@]}"; do
    echo "  - $phase"
done

read -p "Continue with deployment? (y/N): " -n 1 -r

if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Deployment cancelled."
    exit 0
fi

# Phase 1: Azure Infrastructure
echo "==================================================================="
echo "  PHASE 1: AZURE INFRASTRUCTURE"
echo "==================================================================="

if [ -d "terraform" ]; then
    cd terraform
    
    # Initialize if needed
    if [ ! -d ".terraform" ]; then
        echo "Initializing Terraform..."
        terraform init
    fi
    
    # Validate
    echo "Validating configuration..."
    terraform validate
    
    # Plan
    echo "Creating plan..."
    terraform plan -out=tfplan
    
    log_warn "⚠️  REVIEW THE PLAN ABOVE"
    read -p "Apply Terraform plan? (y/N): " -n 1 -r
    
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        terraform apply tfplan
        log_success "✅ Phase 1 complete!"
    else
        log_warn "⚠️  Phase 1 skipped. Run manually: cd terraform && terraform apply"
    fi
    
    cd ..
else
    log_error "❌ Terraform directory not found"
fi

# Phase 2: Kubernetes Resources
echo "==================================================================="
echo "  PHASE 2: KUBERNETES RESOURCES"
echo "==================================================================="

if kubectl cluster-info &> /dev/null; then
    ./scripts/deployment/deploy-infrastructure-phase2.sh
else
    log_warn "⚠️  Kubernetes not accessible. Get kubeconfig first:"
    echo "  az aks get-credentials --resource-group <rg> --name <cluster>"
fi

# Phase 3: Besu Network
echo "==================================================================="
echo "  PHASE 3: BESU NETWORK"
echo "==================================================================="

if kubectl cluster-info &> /dev/null && kubectl get namespace besu-network &> /dev/null; then
    ./scripts/deployment/deploy-infrastructure-phase3.sh
else
    log_warn "⚠️  Kubernetes not ready. Complete Phase 2 first."
fi

# Phase 4: Monitoring
echo "==================================================================="
echo "  PHASE 4: MONITORING AND EXPLORER"
echo "==================================================================="

if kubectl cluster-info &> /dev/null; then
    ./scripts/deployment/deploy-infrastructure-phase4.sh
else
    log_warn "⚠️  Kubernetes not accessible."
fi

# Final verification
echo "==================================================================="
echo "  DEPLOYMENT COMPLETE"
echo "==================================================================="

echo "Running verification..."
./scripts/deployment/verify-chain138-complete.sh

log_success "✅ Infrastructure deployment process complete!"
echo "Next steps:"
echo "  1. Verify all services are running"
echo "  2. Get RPC endpoint"
echo "  3. Deploy contracts"
echo "  4. Run full verification"
