#!/bin/bash
# Comprehensive deployment verification script
# Tests all aspects of the bridge frontend deployment

set -euo pipefail

DOMAIN="cross-all.defi-oracle.io"
BRIDGE_VM_IP="192.168.11.211"
PROXMOX_HOST="192.168.11.10"
VMID="2101"

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }

echo ""
log_info "═══════════════════════════════════════════════════════════"
log_info "  DEPLOYMENT VERIFICATION - Bridge Frontend"
log_info "═══════════════════════════════════════════════════════════"
echo ""

# Test 1: VM Status
log_info "Test 1: Checking VM status..."
VM_STATUS=$(ssh -o ConnectTimeout=10 root@"$PROXMOX_HOST" "pct status $VMID 2>/dev/null | grep -o 'running' || echo 'not-running'")
if [ "$VM_STATUS" = "running" ]; then
    log_success "VM $VMID is running"
else
    log_error "VM $VMID is not running"
    exit 1
fi

# Test 2: Nginx Status
log_info "Test 2: Checking nginx status..."
NGINX_STATUS=$(ssh -o ConnectTimeout=10 root@"$PROXMOX_HOST" "pct exec $VMID -- systemctl is-active nginx 2>/dev/null || echo 'inactive'")
if [ "$NGINX_STATUS" = "active" ]; then
    log_success "Nginx is running"
else
    log_error "Nginx is not running"
    exit 1
fi

# Test 3: Port 80 Listening
log_info "Test 3: Checking port 80..."
PORT_80=$(ssh -o ConnectTimeout=10 root@"$PROXMOX_HOST" "pct exec $VMID -- ss -tlnp 2>/dev/null | grep ':80 ' || echo 'not-listening'")
if [ -n "$PORT_80" ] && [ "$PORT_80" != "not-listening" ]; then
    log_success "Port 80 is listening"
else
    log_error "Port 80 is not listening"
    exit 1
fi

# Test 4: Direct VM IP Access
log_info "Test 4: Testing direct VM IP access..."
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "http://$BRIDGE_VM_IP/" 2>/dev/null || echo "000")
if [ "$HTTP_CODE" = "200" ]; then
    log_success "Direct IP access: HTTP $HTTP_CODE"
else
    log_warn "Direct IP access: HTTP $HTTP_CODE (may be network issue)"
fi

# Test 5: Files Deployed
log_info "Test 5: Verifying deployed files..."
FILE_COUNT=$(ssh -o ConnectTimeout=10 root@"$PROXMOX_HOST" "pct exec $VMID -- find /var/www/html/bridge-dapp -type f 2>/dev/null | wc -l" || echo "0")
if [ "$FILE_COUNT" -gt 0 ]; then
    log_success "Files deployed: $FILE_COUNT files"
else
    log_error "No files found in deployment directory"
    exit 1
fi

# Test 6: Index.html Exists
log_info "Test 6: Checking index.html..."
INDEX_EXISTS=$(ssh -o ConnectTimeout=10 root@"$PROXMOX_HOST" "pct exec $VMID -- test -f /var/www/html/bridge-dapp/index.html && echo 'yes' || echo 'no'" || echo "no")
if [ "$INDEX_EXISTS" = "yes" ]; then
    log_success "index.html exists"
else
    log_error "index.html not found"
    exit 1
fi

# Test 7: DNS Resolution
log_info "Test 7: Checking DNS resolution..."
DNS_IP=$(dig +short "$DOMAIN" 2>/dev/null | head -1 || echo "")
if [ -n "$DNS_IP" ]; then
    log_success "DNS resolves: $DOMAIN → $DNS_IP"
else
    log_warn "DNS not configured or not resolvable"
fi

# Test 8: Domain HTTP Access
log_info "Test 8: Testing domain HTTP access..."
DOMAIN_HTTP=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "http://$DOMAIN/" 2>/dev/null || echo "000")
if [ "$DOMAIN_HTTP" = "200" ] || [ "$DOMAIN_HTTP" = "301" ] || [ "$DOMAIN_HTTP" = "302" ]; then
    log_success "Domain HTTP: $DOMAIN_HTTP"
else
    log_warn "Domain HTTP: $DOMAIN_HTTP (may need NPMplus configuration)"
fi

# Test 9: Domain HTTPS Access
log_info "Test 9: Testing domain HTTPS access..."
DOMAIN_HTTPS=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 -k "https://$DOMAIN/" 2>/dev/null || echo "000")
if [ "$DOMAIN_HTTPS" = "200" ]; then
    log_success "Domain HTTPS: $DOMAIN_HTTPS ✅"
else
    log_warn "Domain HTTPS: $DOMAIN_HTTPS (may need SSL certificate)"
fi

# Test 10: Admin Panel Access
log_info "Test 10: Testing admin panel access..."
ADMIN_HTTP=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "http://$DOMAIN/admin" 2>/dev/null || echo "000")
if [ "$ADMIN_HTTP" = "200" ]; then
    log_success "Admin panel HTTP: $ADMIN_HTTP"
else
    log_warn "Admin panel HTTP: $ADMIN_HTTP"
fi

# Summary
echo ""
log_info "═══════════════════════════════════════════════════════════"
log_info "  VERIFICATION SUMMARY"
log_info "═══════════════════════════════════════════════════════════"
echo ""

if [ "$VM_STATUS" = "running" ] && [ "$NGINX_STATUS" = "active" ] && [ "$INDEX_EXISTS" = "yes" ]; then
    log_success "✅ Core deployment: PASSED"
else
    log_error "❌ Core deployment: FAILED"
fi

if [ "$DOMAIN_HTTPS" = "200" ]; then
    log_success "✅ Production access: PASSED"
    echo ""
    log_success "🌐 Access your deployment:"
    log_success "   https://$DOMAIN/"
    log_success "   https://$DOMAIN/admin"
elif [ "$DOMAIN_HTTP" = "200" ] || [ "$DOMAIN_HTTP" = "301" ] || [ "$DOMAIN_HTTP" = "302" ]; then
    log_warn "⚠️  Production access: HTTP only (SSL needed)"
    echo ""
    log_info "🌐 Access your deployment:"
    log_info "   http://$DOMAIN/ (HTTP)"
    log_info "   https://$DOMAIN/ (SSL certificate needed)"
else
    log_warn "⚠️  Production access: Not yet configured"
    echo ""
    log_info "📋 Next steps:"
    log_info "   1. Configure NPMplus proxy host (see NPMPLUS_CONFIGURATION.md)"
    log_info "   2. Configure DNS A record if needed"
    log_info "   3. Request SSL certificate via NPMplus"
fi

echo ""
log_success "Direct access: http://$BRIDGE_VM_IP/ (HTTP $HTTP_CODE)"

echo ""
