#!/usr/bin/env bash
# Safely prune old/obsolete documentation and content
# Creates a backup before deletion and provides detailed logging

set -euo pipefail

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

# Create backup directory
BACKUP_DIR="$PROJECT_ROOT/backup-old-docs-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$BACKUP_DIR"

echo "=== Pruning Old Documentation ==="
echo "Backup directory: $BACKUP_DIR"
echo ""

# List of historical/obsolete documents to remove (with confirmation)
OBSOLETE_DOCS=(
    # Historical VMID migration docs (superseded by current ranges)
    "docs/HISTORICAL_VMID_REFERENCES.md"
    "docs/VMID_UPDATE_COMPLETE.md"
    "docs/VMID_REFERENCE_AUDIT.md"
    "docs/VMID_ALLOCATION.md"  # Superseded by VMID_ALLOCATION_FINAL.md
    
    # Old deployment status/completion reports
    "docs/DEPLOYMENT_STATUS.md"
    "docs/DEPLOYMENT_REVIEW_COMPLETE.md"
    "docs/DEPLOYMENT_REVIEW.md"
    "docs/DEPLOYMENT_TIME_ESTIMATE.md"
    "docs/DEPLOYMENT_TIME_ESTIMATE_BESU_ONLY.md"
    "docs/DEPLOYMENT_VALIDATION_REPORT.md"
    "docs/DEPLOYED_VMIDS_LIST.md"
    "docs/DEPLOYMENT_OPTIMIZATION_COMPLETE.md"
    "docs/DEPLOYMENT_OPTIMIZATION_RECOMMENDATIONS.md"
    "docs/DEPLOYMENT_RECOMMENDATIONS_STATUS.md"
    "docs/DEPLOYMENT_CONFIGURATION_VERIFICATION.md"
    
    # Old status/completion reports
    "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"
    
    # Old review documents
    "docs/REVIEW_INCONSISTENCIES_GAPS.md"
    "docs/REVIEW_SUMMARY.md"
    "docs/COMPREHENSIVE_REVIEW.md"
    "docs/FINAL_REVIEW.md"
    "docs/DETAILED_ISSUES_REVIEW.md"
    "docs/RECOMMENDATIONS_OVERVIEW.md"
    
    # OS template analysis (historical)
    "docs/OS_TEMPLATE_CHANGE.md"
    "docs/UBUNTU_DEBIAN_ANALYSIS.md"
    "docs/OS_TEMPLATE_ANALYSIS.md"
    
    # Old DHCP documentation (containers now use static IPs)
    "docs/DHCP_IP_ADDRESSES.md"
)

echo "Files to be removed (will be backed up first):"
echo ""
for doc in "${OBSOLETE_DOCS[@]}"; do
    if [[ -f "$doc" ]]; then
        echo "  - $doc"
    fi
done

echo ""
echo "⚠️  WARNING: This will remove the files listed above"
echo "   All files will be backed up to: $BACKUP_DIR"
echo ""

read -p "Continue with pruning? (yes/no): " -r
if [[ ! $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
    echo "Pruning cancelled"
    exit 0
fi

echo ""
echo "Creating backups and removing files..."

removed_count=0
skipped_count=0

for doc in "${OBSOLETE_DOCS[@]}"; do
    if [[ -f "$doc" ]]; then
        # Create backup
        backup_path="$BACKUP_DIR/$doc"
        mkdir -p "$(dirname "$backup_path")"
        cp "$doc" "$backup_path"
        
        # Remove original
        rm "$doc"
        echo "  ✅ Removed: $doc (backed up)"
        removed_count=$((removed_count + 1))
    else
        skipped_count=$((skipped_count + 1))
    fi
done

echo ""
echo "=== Pruning Complete ==="
echo "  Files removed: $removed_count"
echo "  Files skipped (not found): $skipped_count"
echo "  Backup location: $BACKUP_DIR"
echo ""

# Create index of removed files
cat > "$BACKUP_DIR/REMOVED_FILES_INDEX.txt" <<EOF
Removed Documentation Files
Generated: $(date)

Files removed:
$(for doc in "${OBSOLETE_DOCS[@]}"; do echo "  - $doc"; done)

Total removed: $removed_count
EOF

echo "Backup index created: $BACKUP_DIR/REMOVED_FILES_INDEX.txt"

