#!/usr/bin/env bash
# Complete deployment and fix for Blockscout Explorer
# This script deploys Blockscout if needed, then fixes all configuration issues
# Usage: ./deploy-and-fix-blockscout.sh [VMID] [IP]
# Defaults: VMID=5000, IP=192.168.11.140

set -euo pipefail

VMID="${1:-5000}"
IP="${2:-192.168.11.140}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
log_section() { echo -e "${CYAN}════════════════════════════════════════${NC}"; }

PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"

log_section
log_info "Blockscout Explorer - Complete Deployment and Fix"
log_info "VMID: $VMID"
log_info "IP: $IP"
log_section
echo ""

# Check if container exists
log_section
log_info "Checking if container exists..."
log_section

CONTAINER_EXISTS=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pct list | grep -E '^\s*$VMID\s' >/dev/null 2>&1 && echo 'yes' || echo 'no'")

if [ "$CONTAINER_EXISTS" = "no" ]; then
    log_warn "Container $VMID does not exist. Deploying..."
    
    # Check if we're on the Proxmox host or remote
    if command -v pct >/dev/null 2>&1; then
        log_info "Running on Proxmox host - deploying directly..."
        DEPLOY_SCRIPT="$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/deployment/deploy-explorer.sh"
        if [ -f "$DEPLOY_SCRIPT" ]; then
            # Source config if available
            if [ -f "$PROJECT_ROOT/smom-dbis-138-proxmox/config/proxmox.conf" ]; then
                source "$PROJECT_ROOT/smom-dbis-138-proxmox/config/proxmox.conf" || true
            fi
            export VMID_EXPLORER_START="$VMID"
            export PUBLIC_SUBNET="${PUBLIC_SUBNET:-192.168.11}"
            bash "$DEPLOY_SCRIPT" || {
                log_error "Deployment failed"
                exit 1
            }
        else
            log_error "Deployment script not found: $DEPLOY_SCRIPT"
            exit 1
        fi
    else
        log_warn "Not running on Proxmox host. Please deploy manually:"
        log_info ""
        log_info "  ssh root@$PROXMOX_HOST"
        log_info "  cd /home/intlc/projects/proxmox/smom-dbis-138-proxmox/scripts/deployment"
        log_info "  export VMID_EXPLORER_START=$VMID"
        log_info "  export PUBLIC_SUBNET=192.168.11"
        log_info "  ./deploy-explorer.sh"
        log_info ""
        log_info "After deployment, run this script again to configure."
        exit 1
    fi
    
    log_success "Container deployed. Waiting 10 seconds for it to start..."
    sleep 10
else
    log_success "Container already exists"
fi

# Now run the fix script
log_section
log_info "Running fix script..."
log_section

"$SCRIPT_DIR/fix-blockscout-explorer.sh" "$VMID" "$IP"

