#!/usr/bin/env bash
# Comprehensive Review Script
# Checks for inconsistencies, gaps, and dependency issues across the project

set -euo pipefail

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'
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"; }

REPORT_FILE="$PROJECT_ROOT/logs/comprehensive-review-$(date +%Y%m%d-%H%M%S).md"
mkdir -p "$PROJECT_ROOT/logs"

{
cat << 'EOF'
# Comprehensive Project Review Report

Generated: $(date)

## Summary

This report identifies:
- VMID inconsistencies
- IP address inconsistencies  
- Configuration gaps
- Missing dependencies
- Unreferenced or obsolete files

---

## 1. VMID Consistency

### Expected VMID Ranges
- **Validators**: 1000-1004 (5 nodes)
- **Sentries**: 1500-1503 (4 nodes)
- **RPC**: 2500-2502 (3 nodes)

### Issues Found

EOF

echo "### Files with Old VMIDs (106-117)"
echo ""
grep -rE "\b(106|107|108|109|110|111|112|113|114|115|116|117)\b" \
  "$PROJECT_ROOT/smom-dbis-138-proxmox/" "$PROJECT_ROOT/docs/" 2>/dev/null | \
  grep -v node_modules | grep -v ".git" | \
  grep -v "EXPECTED_CONTAINERS.md" | \
  grep -v "VMID_ALLOCATION.md" | \
  grep -v "HISTORICAL" | \
  cut -d: -f1 | sort -u | while read -r file; do
    echo "- \`$file\`"
done

echo ""
echo "---"
echo ""
echo "## 2. IP Address Consistency"
echo ""
echo "### Expected IP Range"
echo "- Base subnet: 192.168.11.0/24"
echo "- Validators: 192.168.11.100-104"
echo "- Sentries: 192.168.11.150-153"
echo "- RPC: 192.168.11.250-252"
echo ""

echo "### Files with Old IPs (10.3.1.X)"
echo ""
grep -rE "10\.3\.1\." "$PROJECT_ROOT/smom-dbis-138-proxmox/" "$PROJECT_ROOT/docs/" 2>/dev/null | \
  grep -v node_modules | grep -v ".git" | \
  cut -d: -f1 | sort -u | while read -r file; do
    echo "- \`$file\`"
done

echo ""
echo "---"
echo ""
echo "## 3. Configuration Gaps"
echo ""

# Check for missing config files
if [[ ! -f "$PROJECT_ROOT/smom-dbis-138-proxmox/config/proxmox.conf" ]]; then
    echo "- ❌ Missing: \`config/proxmox.conf\`"
else
    echo "- ✅ Found: \`config/proxmox.conf\`"
fi

if [[ ! -f "$PROJECT_ROOT/smom-dbis-138-proxmox/config/network.conf" ]]; then
    echo "- ❌ Missing: \`config/network.conf\`"
else
    echo "- ✅ Found: \`config/network.conf\`"
fi

echo ""
echo "### Key Configuration Variables Check"
echo ""

# Source config to check variables
if [[ -f "$PROJECT_ROOT/smom-dbis-138-proxmox/config/proxmox.conf" ]]; then
    source "$PROJECT_ROOT/smom-dbis-138-proxmox/config/proxmox.conf" 2>/dev/null || true
    
    if [[ "${VALIDATOR_COUNT:-}" != "5" ]]; then
        echo "- ⚠️  VALIDATOR_COUNT=${VALIDATOR_COUNT:-not set} (expected: 5)"
    else
        echo "- ✅ VALIDATOR_COUNT=5"
    fi
    
    if [[ "${SENTRY_COUNT:-}" != "4" ]]; then
        echo "- ⚠️  SENTRY_COUNT=${SENTRY_COUNT:-not set} (expected: 4)"
    else
        echo "- ✅ SENTRY_COUNT=4"
    fi
    
    if [[ "${RPC_COUNT:-}" != "3" ]]; then
        echo "- ⚠️  RPC_COUNT=${RPC_COUNT:-not set} (expected: 3)"
    else
        echo "- ✅ RPC_COUNT=3"
    fi
fi

echo ""
echo "---"
echo ""
echo "## 4. Dependencies Review"
echo ""

echo "### Required Tools"
echo ""

# Check for required tools
REQUIRED_TOOLS=(
    "pct:Proxmox Container Toolkit"
    "jq:JSON processor"
    "sshpass:SSH password authentication"
    "timeout:Command timeout utility"
    "openssl:OpenSSL toolkit"
    "curl:HTTP client"
    "wget:File downloader"
)

for tool_info in "${REQUIRED_TOOLS[@]}"; do
    tool=$(echo "$tool_info" | cut -d: -f1)
    desc=$(echo "$tool_info" | cut -d: -f2)
    if command -v "$tool" >/dev/null 2>&1; then
        echo "- ✅ $tool ($desc)"
    else
        echo "- ❌ $tool ($desc) - MISSING"
    fi
done

echo ""
echo "### Optional Tools"
echo ""

OPTIONAL_TOOLS=(
    "quorum-genesis-tool:Genesis configuration generator"
    "besu:Hyperledger Besu client"
)

for tool_info in "${OPTIONAL_TOOLS[@]}"; do
    tool=$(echo "$tool_info" | cut -d: -f1)
    desc=$(echo "$tool_info" | cut -d: -f2)
    if command -v "$tool" >/dev/null 2>&1; then
        echo "- ✅ $tool ($desc)"
    else
        echo "- ⚠️  $tool ($desc) - Optional (for key generation)"
    fi
done

echo ""
echo "---"
echo ""
echo "## 5. Script Dependencies"
echo ""

echo "### Scripts Checking for Dependencies"
echo ""

# Find scripts that check for tools
grep -rE "(command -v|which|command_exists)" "$PROJECT_ROOT/smom-dbis-138-proxmox/scripts/" 2>/dev/null | \
  grep -v ".git" | cut -d: -f1 | sort -u | while read -r file; do
    echo "- \`$file\`"
done

echo ""
echo "---"
echo ""
echo "## 6. Missing or Incomplete Files"
echo ""

# Check for common missing files
MISSING_CHECKS=(
    "smom-dbis-138-proxmox/scripts/copy-besu-config.sh:Configuration copy script"
    "smom-dbis-138-proxmox/scripts/network/bootstrap-network.sh:Network bootstrap script"
    "smom-dbis-138-proxmox/scripts/validation/validate-deployment-comprehensive.sh:Deployment validation script"
)

for check in "${MISSING_CHECKS[@]}"; do
    file=$(echo "$check" | cut -d: -f1)
    desc=$(echo "$check" | cut -d: -f2)
    if [[ -f "$PROJECT_ROOT/$file" ]]; then
        echo "- ✅ $desc: \`$file\`"
    else
        echo "- ❌ Missing: $desc - \`$file\`"
    fi
done

echo ""
echo "---"
echo ""
echo "## 7. Documentation Inconsistencies"
echo ""

echo "### Documents with Outdated VMID References"
echo ""

# Check documentation files
OLD_VMID_DOCS=(
    "docs/EXPECTED_CONTAINERS.md:References old VMIDs (106-117)"
    "docs/VMID_ALLOCATION.md:Contains historical VMID ranges (1100-1122)"
)

for doc_info in "${OLD_VMID_DOCS[@]}"; do
    doc=$(echo "$doc_info" | cut -d: -f1)
    issue=$(echo "$doc_info" | cut -d: -f2)
    if [[ -f "$PROJECT_ROOT/$doc" ]]; then
        echo "- ⚠️  \`$doc\` - $issue"
    fi
done

echo ""
echo "---"
echo ""
echo "## Recommendations"
echo ""
echo "1. Update files with old VMID references to use current ranges (1000-1004, 1500-1503, 2500-2502)"
echo "2. Update files with old IP addresses (10.3.1.X) to use new range (192.168.11.X)"
echo "3. Review and update historical documentation files"
echo "4. Ensure all required tools are installed on deployment hosts"
echo "5. Verify configuration file consistency across all scripts"
echo ""

} > "$REPORT_FILE"

log_info "========================================="
log_info "Comprehensive Review Complete"
log_info "========================================="
log_info "Report saved to: $REPORT_FILE"
log_info ""
cat "$REPORT_FILE"

