#!/usr/bin/env bash
# Prune Historical and Obsolete Documentation
# Marks historical docs and removes truly obsolete files

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

DRY_RUN="${DRY_RUN:-true}"

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --execute)
            DRY_RUN=false
            shift
            ;;
        --help)
            cat << EOF
Usage: $0 [OPTIONS]

Prune historical and obsolete documentation files.

Options:
  --execute        Actually delete files (default: dry-run)
  --help           Show this help

EOF
            exit 0
            ;;
        *)
            log_error "Unknown option: $1"
            exit 1
            ;;
    esac
done

log_info "========================================="
log_info "Prune Historical Documentation"
log_info "========================================="
log_info "Mode: $([ "$DRY_RUN" == "true" ] && echo "DRY-RUN" || echo "EXECUTE")"
log_info ""

# Files to mark as historical (add header)
HISTORICAL_FILES=(
    "docs/EXPECTED_CONTAINERS.md"
    "docs/VMID_ALLOCATION.md"
    "docs/VMID_REFERENCE_AUDIT.md"
    "docs/VMID_UPDATE_COMPLETE.md"
)

# Files to delete (truly obsolete, superseded by current docs)
OBSOLETE_FILES=(
    # Status/completion reports (old, superseded)
    "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"
    
    # VMID consistency reports (superseded by current state)
    "docs/VMID_CONSISTENCY_REPORT.md"
    "docs/ACTIVE_DOCS_UPDATE_SUMMARY.md"
    "docs/PROJECT_UPDATE_COMPLETE.md"
)

# Mark historical files with header
log_info "=== Marking Historical Documentation ==="
for file in "${HISTORICAL_FILES[@]}"; do
    if [[ -f "$PROJECT_ROOT/$file" ]]; then
        # Check if already marked
        if grep -q "^<!-- HISTORICAL: This document contains historical" "$PROJECT_ROOT/$file" 2>/dev/null; then
            log_info "Already marked: $file"
        else
            if [[ "$DRY_RUN" == "true" ]]; then
                log_info "Would mark as historical: $file"
            else
                # Add header comment
                if [[ "$file" == *.md ]]; then
                    # For markdown, add HTML comment at top
                    TEMP_FILE=$(mktemp)
                    {
                        echo "<!-- HISTORICAL: This document contains historical VMID ranges and is kept for reference only. Current ranges: Validators 1000-1004, Sentries 1500-1503, RPC 2500-2502 -->"
                        echo ""
                        cat "$PROJECT_ROOT/$file"
                    } > "$TEMP_FILE"
                    mv "$TEMP_FILE" "$PROJECT_ROOT/$file"
                    log_success "Marked as historical: $file"
                fi
            fi
        fi
    else
        log_warn "File not found: $file"
    fi
done

echo ""

# Delete obsolete files
log_info "=== Removing Obsolete Documentation ==="
DELETED_COUNT=0
NOT_FOUND_COUNT=0

for file in "${OBSOLETE_FILES[@]}"; do
    if [[ -f "$PROJECT_ROOT/$file" ]]; then
        if [[ "$DRY_RUN" == "true" ]]; then
            log_info "Would delete: $file"
            DELETED_COUNT=$((DELETED_COUNT + 1))
        else
            if rm -f "$PROJECT_ROOT/$file" 2>/dev/null; then
                log_success "Deleted: $file"
                DELETED_COUNT=$((DELETED_COUNT + 1))
            else
                log_error "Failed to delete: $file"
            fi
        fi
    else
        NOT_FOUND_COUNT=$((NOT_FOUND_COUNT + 1))
    fi
done

echo ""

# Summary
log_info "========================================="
log_info "Summary"
log_info "========================================="
log_info "Historical files marked: ${#HISTORICAL_FILES[@]}"
log_info "Obsolete files $([ "$DRY_RUN" == "true" ] && echo "to delete" || echo "deleted"): $DELETED_COUNT"
log_info "Files not found: $NOT_FOUND_COUNT"
echo ""

if [[ "$DRY_RUN" == "true" ]]; then
    log_warn "DRY-RUN mode: No files were modified"
    log_info "Run with --execute to actually mark/delete files"
else
    log_success "Cleanup completed"
fi

