#!/usr/bin/env bash
# Review project content and identify old/outdated information to prune
# Focuses on old VMID references, obsolete documentation, and outdated configurations

set -euo pipefail

PROJECT_ROOT="/home/intlc/projects/proxmox"
cd "$PROJECT_ROOT"

echo "=== Project Content Review and Pruning Analysis ==="
echo ""

# Current valid VMID ranges (DO NOT PRUNE)
CURRENT_VALIDATORS="1000-1004"
CURRENT_SENTRIES="1500-1503"
CURRENT_RPC="2500-2502"
CURRENT_INFRASTRUCTURE="100-105"

# Old VMID ranges that should be removed/updated
OLD_VMIDS="106|107|108|109|110|111|112|113|114|115|116|117|118|119|120|121|122|123"
OLD_RANGES="106-110|111-114|115-117|120-129|130-139"

echo "Current Valid VMID Ranges:"
echo "  Infrastructure: $CURRENT_INFRASTRUCTURE (KEEP)"
echo "  Validators: $CURRENT_VALIDATORS"
echo "  Sentries: $CURRENT_SENTRIES"
echo "  RPC: $CURRENT_RPC"
echo ""

echo "=== Files with Old VMID References ==="
echo ""

# Find files with old VMID references
echo "Searching for files referencing old VMIDs ($OLD_VMIDS)..."
OLD_VMID_FILES=$(grep -rE "\b($OLD_VMIDS)\b" --include="*.md" --include="*.sh" --include="*.js" --include="*.py" --include="*.conf" --include="*.example" \
    smom-dbis-138-proxmox/ docs/ scripts/ 2>/dev/null | cut -d: -f1 | sort -u)

if [[ -n "$OLD_VMID_FILES" ]]; then
    echo "Found $(echo "$OLD_VMID_FILES" | wc -l) files with old VMID references:"
    echo ""
    for file in $OLD_VMID_FILES; do
        # Skip if file references current VMIDs too (may be migration docs)
        if grep -qE "\b(1000|1001|1002|1003|1004|1500|1501|1502|1503|2500|2501|2502)\b" "$file" 2>/dev/null; then
            echo "  ⚠️  $file (has both old and new VMIDs - migration/historical doc?)"
        else
            echo "  ❌ $file (old VMIDs only - candidate for update/removal)"
        fi
    done
else
    echo "  ✅ No files found with old VMID references"
fi

echo ""
echo "=== Historical/Migration Documents (May be obsolete) ==="
echo ""

HISTORICAL_DOCS=(
    "docs/HISTORICAL_VMID_REFERENCES.md"
    "docs/VMID_UPDATE_COMPLETE.md"
    "docs/VMID_REFERENCE_AUDIT.md"
    "docs/VMID_ALLOCATION.md"
    "docs/OS_TEMPLATE_CHANGE.md"
    "docs/UBUNTU_DEBIAN_ANALYSIS.md"
    "docs/OS_TEMPLATE_ANALYSIS.md"
    "docs/DEPLOYMENT_REVIEW_COMPLETE.md"
    "docs/DEPLOYMENT_REVIEW.md"
    "docs/DEPLOYMENT_STATUS.md"
    "docs/DEPLOYMENT_OPTIMIZATION_COMPLETE.md"
    "docs/DEPLOYMENT_TIME_ESTIMATE.md"
    "docs/DEPLOYMENT_TIME_ESTIMATE_BESU_ONLY.md"
    "docs/DEPLOYMENT_VALIDATION_REPORT.md"
    "docs/DEPLOYMENT_VALIDATION_REQUIREMENTS.md"
    "docs/DEPLOYED_VMIDS_LIST.md"
    "docs/NEXT_STEPS_COMPREHENSIVE.md"
    "docs/NEXT_STEPS_COMPLETE.md"
    "docs/NEXT_STEPS_SUMMARY.md"
    "docs/COMPLETION_REPORT.md"
    "docs/FIXES_APPLIED.md"
    "docs/REVIEW_FIXES_APPLIED.md"
    "docs/MINOR_OBSERVATIONS_FIXED.md"
    "docs/NON_CRITICAL_FIXES_COMPLETE.md"
    "docs/QUICK_WINS_COMPLETED.md"
    "docs/TASK_COMPLETION_SUMMARY.md"
    "docs/IMPLEMENTATION_COMPLETE.md"
    "docs/PREREQUISITES_COMPLETE.md"
    "docs/SETUP_COMPLETE.md"
    "docs/SETUP_COMPLETE_FINAL.md"
    "docs/SETUP_STATUS.md"
    "docs/VALIDATION_STATUS.md"
    "docs/CONFIGURATION_ALIGNMENT.md"
    "docs/DEPLOYMENT_CONFIGURATION_VERIFICATION.md"
    "docs/REVIEW_INCONSISTENCIES_GAPS.md"
    "docs/REVIEW_SUMMARY.md"
    "docs/COMPREHENSIVE_REVIEW.md"
    "docs/FINAL_REVIEW.md"
    "docs/DETAILED_ISSUES_REVIEW.md"
    "docs/DEPLOYMENT_OPTIMIZATION_RECOMMENDATIONS.md"
    "docs/DEPLOYMENT_RECOMMENDATIONS_STATUS.md"
    "docs/RECOMMENDATIONS_OVERVIEW.md"
)

for doc in "${HISTORICAL_DOCS[@]}"; do
    if [[ -f "$doc" ]]; then
        echo "  📄 $doc"
    fi
done

echo ""
echo "=== Duplicate/Similar Documentation Files ==="
echo ""

# Find potentially duplicate documentation
DUPLICATE_PATTERNS=(
    "*QUICK_START*"
    "*DEPLOYMENT*"
    "*NEXT_STEPS*"
    "*REVIEW*"
    "*COMPLETE*"
    "*STATUS*"
)

for pattern in "${DUPLICATE_PATTERNS[@]}"; do
    files=$(find docs/ smom-dbis-138-proxmox/docs/ -name "$pattern" -type f 2>/dev/null | sort)
    if [[ -n "$files" ]]; then
        count=$(echo "$files" | wc -l)
        if [[ $count -gt 1 ]]; then
            echo "  Found $count files matching '$pattern':"
            echo "$files" | sed 's/^/    /'
            echo ""
        fi
    fi
done

echo "=== Outdated Configuration Examples ==="
echo ""

# Check for old config examples
if [[ -f "smom-dbis-138-proxmox/config/proxmox.conf.example" ]]; then
    if grep -qE "\b(106|110|115|120)\b" smom-dbis-138-proxmox/config/proxmox.conf.example 2>/dev/null; then
        echo "  ⚠️  smom-dbis-138-proxmox/config/proxmox.conf.example contains old VMID defaults"
    fi
fi

echo ""
echo "=== Summary and Recommendations ==="
echo ""

echo "Files to REVIEW for removal/update:"
echo "  1. Historical migration documents (if no longer needed)"
echo "  2. Duplicate documentation files"
echo "  3. Files with old VMID references that don't also reference current ranges"
echo "  4. Old configuration examples"
echo ""

echo "Files to KEEP (may have old references but are historical):"
echo "  - Migration/historical reference documents (for context)"
echo "  - Current active documentation (even if examples need updating)"
echo "  - Configuration files in use"
echo ""

echo "=== Next Steps ==="
echo ""
echo "1. Review historical documents list above - decide which to archive/delete"
echo "2. Update active documentation with old VMID references"
echo "3. Update configuration examples to use current VMID ranges"
echo "4. Remove or archive obsolete status/completion reports"
echo ""
echo "To safely remove files, use:"
echo "  ./scripts/prune-old-documentation.sh"

