#!/usr/bin/env bash
# Phased Deployment Orchestrator
# Deploys infrastructure in phases: Besu → CCIP → Other Services
# Allows validation between phases to reduce risk

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Try to find project root - could be at same level or in smom-dbis-138-proxmox subdirectory
if [[ -d "$SCRIPT_DIR/../../smom-dbis-138-proxmox" ]]; then
    PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../smom-dbis-138-proxmox" && pwd)"
elif [[ -d "$SCRIPT_DIR/../.." ]]; then
    PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
else
    PROJECT_ROOT="$SCRIPT_DIR/../.."
fi

source "$PROJECT_ROOT/lib/common.sh" 2>/dev/null || {
    log_info() { echo "[INFO] $1"; }
    log_success() { echo "[✓] $1"; }
    log_error() { echo "[ERROR] $1"; exit 1; }
    log_warn() { echo "[WARN] $1"; }
}
source "$PROJECT_ROOT/lib/progress-tracking.sh" 2>/dev/null || true

# Load configuration
load_config "$PROJECT_ROOT/config/proxmox.conf" 2>/dev/null || true

# Command line options
SKIP_PHASE1="${SKIP_PHASE1:-false}"
SKIP_PHASE2="${SKIP_PHASE2:-false}"
SKIP_PHASE3="${SKIP_PHASE3:-false}"
SKIP_VALIDATION="${SKIP_VALIDATION:-false}"
SOURCE_PROJECT="${SOURCE_PROJECT:-}"

while [[ $# -gt 0 ]]; do
    case $1 in
        --skip-phase1)
            SKIP_PHASE1=true
            shift
            ;;
        --skip-phase2)
            SKIP_PHASE2=true
            shift
            ;;
        --skip-phase3)
            SKIP_PHASE3=true
            shift
            ;;
        --skip-validation)
            SKIP_VALIDATION=true
            shift
            ;;
        --source-project)
            SOURCE_PROJECT="$2"
            shift 2
            ;;
        --help)
            echo "Usage: $0 [OPTIONS]"
            echo ""
            echo "Phased Deployment Orchestrator"
            echo ""
            echo "Phases:"
            echo "  1. Besu Network (12 containers) - 1.5-2.5 hours"
            echo "  2. CCIP Network (41-43 containers) - 2.5-4 hours"
            echo "  3. Other Services (14 containers) - 1.5-2.5 hours"
            echo ""
            echo "Options:"
            echo "  --skip-phase1       Skip Besu network deployment"
            echo "  --skip-phase2       Skip CCIP network deployment"
            echo "  --skip-phase3       Skip other services deployment"
            echo "  --skip-validation   Skip validation between phases"
            echo "  --source-project PATH  Path to source project with config files"
            echo "  --help              Show this help message"
            exit 0
            ;;
        *)
            log_error "Unknown option: $1"
            exit 1
            ;;
    esac
done

log_info "========================================="
log_info "Phased Deployment Orchestrator"
log_info "========================================="
log_info ""

# Check prerequisites
if ! command_exists pct; then
    log_error "pct command not found. This script must be run on Proxmox host."
fi

if [[ $EUID -ne 0 ]]; then
    log_error "This script must be run as root"
fi

# Helper function to find script
find_script() {
    local script_name="$1"
    # Try current directory first
    if [[ -f "$SCRIPT_DIR/$script_name" ]]; then
        echo "$SCRIPT_DIR/$script_name"
    # Try PROJECT_ROOT scripts/deployment
    elif [[ -f "$PROJECT_ROOT/scripts/deployment/$script_name" ]]; then
        echo "$PROJECT_ROOT/scripts/deployment/$script_name"
    # Try smom-dbis-138-proxmox path
    elif [[ -f "$(dirname "$SCRIPT_DIR")/smom-dbis-138-proxmox/scripts/deployment/$script_name" ]]; then
        echo "$(dirname "$SCRIPT_DIR")/smom-dbis-138-proxmox/scripts/deployment/$script_name"
    else
        echo ""
    fi
}

# Pre-cache OS template (recommendation)
log_info "=== Pre-caching OS Template ==="
PRE_CACHE_SCRIPT=$(find_script "pre-cache-os-template.sh")
if [[ -n "$PRE_CACHE_SCRIPT" ]] && [[ -f "$PRE_CACHE_SCRIPT" ]]; then
    "$PRE_CACHE_SCRIPT" || log_warn "Template pre-caching had issues, continuing..."
else
    log_warn "pre-cache-os-template.sh not found, skipping template pre-cache"
fi

# Phase 1: Besu Network
if [[ "$SKIP_PHASE1" != "true" ]]; then
    log_info ""
    log_info "========================================="
    log_info "PHASE 1: Besu Network Deployment"
    log_info "========================================="
    log_info "Containers: 11 (4 validators, 4 sentries, 3 RPC)"
    log_info "Estimated time: 90-150 minutes (1.5-2.5 hours)"
    log_info ""
    
    DEPLOY_BESU_SCRIPT=$(find_script "deploy-besu-nodes.sh")
    if [[ -n "$DEPLOY_BESU_SCRIPT" ]] && [[ -f "$DEPLOY_BESU_SCRIPT" ]]; then
        if "$DEPLOY_BESU_SCRIPT"; then
            log_success "Phase 1 completed successfully"
        else
            log_error "Phase 1 failed. Fix issues before continuing."
            exit 1
        fi
    else
        log_error "deploy-besu-nodes.sh not found in $SCRIPT_DIR or $PROJECT_ROOT/scripts/deployment"
        exit 1
    fi
    
    # Copy configuration files
    if [[ -n "$SOURCE_PROJECT" ]] && [[ -d "$SOURCE_PROJECT" ]]; then
        log_info ""
        log_info "Copying Besu configuration files..."
        if [[ -f "$PROJECT_ROOT/scripts/copy-besu-config-with-nodes.sh" ]]; then
            SOURCE_PROJECT="$SOURCE_PROJECT" "$PROJECT_ROOT/scripts/copy-besu-config-with-nodes.sh" || {
                log_error "Failed to copy configuration files"
            }
        fi
    fi
    
    # Validation after Phase 1
    if [[ "$SKIP_VALIDATION" != "true" ]]; then
        log_info ""
        log_info "=== Phase 1 Validation ==="
        if [[ -f "$PROJECT_ROOT/scripts/validation/validate-deployment-comprehensive.sh" ]]; then
            "$PROJECT_ROOT/scripts/validation/validate-deployment-comprehensive.sh" || {
                log_warn "Phase 1 validation had issues. Review before continuing to Phase 2."
                read -p "Continue to Phase 2? (y/N): " -n 1 -r
                echo
                if [[ ! $REPLY =~ ^[Yy]$ ]]; then
                    log_error "Deployment paused. Fix Phase 1 issues before continuing."
                fi
            }
        fi
    fi
else
    log_info "Skipping Phase 1 (Besu Network)"
fi

# Phase 2: CCIP Network
if [[ "$SKIP_PHASE2" != "true" ]]; then
    log_info ""
    log_info "========================================="
    log_info "PHASE 2: CCIP Network Deployment"
    log_info "========================================="
    log_info "Containers: 41-43 (2 ops, 2 mon, 16 commit, 16 exec, 5-7 RMN)"
    log_info "Estimated time: 150-240 minutes (2.5-4 hours)"
    log_info ""
    
    DEPLOY_CCIP_SCRIPT=$(find_script "deploy-ccip-nodes.sh")
    if [[ -n "$DEPLOY_CCIP_SCRIPT" ]] && [[ -f "$DEPLOY_CCIP_SCRIPT" ]]; then
        if command_exists init_progress_tracking 2>/dev/null; then
            init_progress_tracking 5 "CCIP Network Deployment"
            update_progress 1 "Deploying CCIP-OPS nodes"
        fi
        if "$DEPLOY_CCIP_SCRIPT"; then
            if command_exists update_progress 2>/dev/null; then
                update_progress 5 "CCIP deployment complete"
                complete_progress
            fi
            log_success "Phase 2 completed successfully"
        else
            log_error "Phase 2 failed. Fix issues before continuing."
            exit 1
        fi
    else
        log_warn "deploy-ccip-nodes.sh not found, skipping CCIP deployment"
    fi
else
    log_info "Skipping Phase 2 (CCIP Network)"
fi

# Phase 3: Other Services
if [[ "$SKIP_PHASE3" != "true" ]]; then
    log_info ""
    log_info "========================================="
    log_info "PHASE 3: Other Services Deployment"
    log_info "========================================="
    log_info "Containers: ~14 (Blockscout, Cacti, Fabric, Firefly, Indy, etc.)"
    log_info "Estimated time: 90-150 minutes (1.5-2.5 hours)"
    log_info ""
    
    # Deploy Hyperledger services
    HYPERLEDGER_SCRIPT=$(find_script "deploy-hyperledger-services.sh")
    if [[ -n "$HYPERLEDGER_SCRIPT" ]] && [[ -f "$HYPERLEDGER_SCRIPT" ]]; then
        log_info "Deploying Hyperledger services..."
        "$HYPERLEDGER_SCRIPT" || log_warn "Hyperledger services had issues"
    fi
    
    # Deploy explorer
    EXPLORER_SCRIPT=$(find_script "deploy-explorer.sh")
    if [[ -n "$EXPLORER_SCRIPT" ]] && [[ -f "$EXPLORER_SCRIPT" ]]; then
        log_info "Deploying Blockscout explorer..."
        "$EXPLORER_SCRIPT" || log_warn "Explorer deployment had issues"
    fi
    
    # Deploy other services
    SERVICES_SCRIPT=$(find_script "deploy-services.sh")
    if [[ -n "$SERVICES_SCRIPT" ]] && [[ -f "$SERVICES_SCRIPT" ]]; then
        log_info "Deploying other services..."
        "$SERVICES_SCRIPT" || log_warn "Services deployment had issues"
    fi
    
    # Deploy monitoring
    MONITORING_SCRIPT=$(find_script "deploy-monitoring.sh")
    if [[ -n "$MONITORING_SCRIPT" ]] && [[ -f "$MONITORING_SCRIPT" ]]; then
        log_info "Deploying monitoring stack..."
        "$MONITORING_SCRIPT" || log_warn "Monitoring deployment had issues"
    fi
    
    log_success "Phase 3 completed"
else
    log_info "Skipping Phase 3 (Other Services)"
fi

# Final validation
if [[ "$SKIP_VALIDATION" != "true" ]]; then
    log_info ""
    log_info "========================================="
    log_info "Final Deployment Validation"
    log_info "========================================="
    if [[ -f "$PROJECT_ROOT/scripts/validation/validate-deployment-comprehensive.sh" ]]; then
        "$PROJECT_ROOT/scripts/validation/validate-deployment-comprehensive.sh"
    fi
fi

log_info ""
log_success "Phased deployment completed!"
log_info ""
log_info "Next steps:"
log_info "  - Verify all services are running"
log_info "  - Check service logs for errors"
log_info "  - Monitor blockchain sync progress"
log_info "  - Configure CCIP DONs (if Phase 2 completed)"

