#!/bin/bash
# Markdown Files Cleanup Script
# Automatically organizes markdown files based on analysis

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$PROJECT_ROOT"

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

# Dry-run mode (set to false to actually move files)
DRY_RUN=${DRY_RUN:-true}

# Log file
LOG_FILE="$PROJECT_ROOT/MARKDOWN_CLEANUP_LOG_$(date +%Y%m%d_%H%M%S).log"

log() {
    echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" | tee -a "$LOG_FILE"
}

warn() {
    echo -e "${YELLOW}[WARN]${NC} $1" | tee -a "$LOG_FILE"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1" | tee -a "$LOG_FILE"
}

success() {
    echo -e "${GREEN}[OK]${NC} $1" | tee -a "$LOG_FILE"
}

move_file() {
    local src="$1"
    local dest="$2"
    local reason="$3"
    
    if [ ! -f "$src" ]; then
        warn "File not found: $src"
        return 1
    fi
    
    # Create destination directory if needed
    local dest_dir=$(dirname "$dest")
    if [ "$DRY_RUN" = "true" ]; then
        log "Would move: $src -> $dest"
        log "  Reason: $reason"
        log "  Would create directory: $dest_dir"
    else
        mkdir -p "$dest_dir"
        if mv "$src" "$dest" 2>/dev/null; then
            success "Moved: $src -> $dest"
            echo "  Reason: $reason" >> "$LOG_FILE"
        else
            error "Failed to move: $src -> $dest"
            return 1
        fi
    fi
}

# Create necessary directories
create_directories() {
    log "Creating directory structure..."
    local dirs=(
        "reports/archive/2026-01-05"
        "reports/status"
        "reports/inventories"
        "reports/analyses"
        "docs/09-troubleshooting/archive"
        "rpc-translator-138/docs/archive"
    )
    
    for dir in "${dirs[@]}"; do
        if [ "$DRY_RUN" = "true" ]; then
            log "Would create: $dir"
        else
            mkdir -p "$dir"
            success "Created: $dir"
        fi
    done
}

# Move timestamped inventory files
move_timestamped_inventories() {
    log "Moving timestamped inventory files..."
    local files=(
        "CONTAINER_INVENTORY_20260105_142214.md"
        "CONTAINER_INVENTORY_20260105_142314.md"
        "CONTAINER_INVENTORY_20260105_142357.md"
        "CONTAINER_INVENTORY_20260105_142455.md"
        "CONTAINER_INVENTORY_20260105_142712.md"
        "CONTAINER_INVENTORY_20260105_142753.md"
        "CONTAINER_INVENTORY_20260105_142842.md"
        "CONTAINER_INVENTORY_20260105_144309.md"
        "CONTAINER_INVENTORY_20260105_153516.md"
        "CONTAINER_INVENTORY_20260105_154200.md"
        "SERVICE_DEPENDENCIES_20260105_143608.md"
        "SERVICE_DEPENDENCIES_20260105_143624.md"
        "IP_AVAILABILITY_20260105_143535.md"
        "DHCP_CONTAINERS_20260105_143507.md"
    )
    
    for file in "${files[@]}"; do
        if [ -f "$file" ]; then
            move_file "$file" "reports/archive/2026-01-05/$file" "Timestamped inventory/report file"
        fi
    done
}

# Move status/completion reports from root to reports
move_status_reports() {
    log "Moving status/completion reports from root..."
    
    # Pattern matching for status reports
    find . -maxdepth 1 -name "*.md" -type f | while read -r file; do
        filename=$(basename "$file")
        
        # Skip essential files
        if [[ "$filename" == "README.md" ]] || [[ "$filename" == "PROJECT_STRUCTURE.md" ]]; then
            continue
        fi
        
        # Check if it's a status/report file
        if [[ "$filename" =~ (STATUS|COMPLETE|FINAL|REPORT|SUMMARY|ANALYSIS|DIAGNOSTIC|INVENTORY) ]]; then
            move_file "$file" "reports/status/$filename" "Status/completion report in root"
        fi
    done
}

# Move VMID-specific reports
move_vmid_reports() {
    log "Moving VMID-specific reports..."
    
    find . -maxdepth 1 -name "VMID*.md" -type f | while read -r file; do
        filename=$(basename "$file")
        move_file "$file" "reports/$filename" "VMID-specific report"
    done
}

# Move IP conflict and network analysis reports
move_network_reports() {
    log "Moving network analysis reports..."
    
    local files=(
        "IP_CONFLICT_ANALYSIS.md"
        "IP_CONFLICT_192.168.11.14_RESOLUTION.md"
        "IP_CONFLICTS_RESOLUTION_COMPLETE.md"
        "VMID_IP_CONFLICTS_ANALYSIS.md"
        "VMID_IP_ADDRESS_LIST.md"
        "FINAL_VMID_IP_MAPPING.md"
        "IP_ASSIGNMENT_PLAN.md"
        "PHASE1_IP_CONFLICT_RESOLUTION.md"
        "PHASE1_IP_INVESTIGATION_COMPLETE.md"
        "PHASE1_IP_INVESTIGATION_STATUS.md"
        "R630-04_IP_CONFLICT_DISCOVERY.md"
        "RESERVED_IP_CONFLICTS_ANALYSIS.md"
        "RESERVED_IP_FIX_COMPLETE.md"
        "RESERVED_IP_FIX_COMPLETE_FINAL.md"
        "RESERVED_IP_FIX_SUMMARY.md"
        "DHCP_CONTAINERS_LIST.md"
        "DHCP_TO_STATIC_CONVERSION_COMPLETE.md"
        "DHCP_TO_STATIC_CONVERSION_FINAL_REPORT.md"
    )
    
    for file in "${files[@]}"; do
        if [ -f "$file" ]; then
            move_file "$file" "reports/analyses/$file" "Network/IP analysis report"
        fi
    done
}

# Move service status reports
move_service_reports() {
    log "Moving service status reports..."
    
    local files=(
        "BLOCK_PRODUCTION_REVIEW.md"
        "BLOCK_PRODUCTION_STATUS.md"
        "SERVICE_VERIFICATION_REPORT.md"
        "RPC_ENDPOINT_DIAGNOSTICS_REPORT.md"
        "RPC_SSL_ISSUE_SUMMARY.md"
        "RPC_TRANSACTION_FAILURE_INVESTIGATION.md"
        "RPC_TRANSACTION_FAILURE_ROOT_CAUSE.md"
        "BESU_*.md"
        "FIREFLY_*.md"
        "DBIS_*.md"
        "EXPLORER_*.md"
        "BLOCKSCOUT_*.md"
    )
    
    # Handle specific files
    for pattern in "${files[@]}"; do
        find . -maxdepth 1 -name "$pattern" -type f | while read -r file; do
            filename=$(basename "$file")
            # Skip if it's a script or config file
            if [[ ! "$filename" =~ (\.sh|\.py|\.js|\.json)$ ]]; then
                move_file "$file" "reports/status/$filename" "Service status report"
            fi
        done
    done
}

# Move temporary fix guides from rpc-translator-138
move_rpc_translator_temp_files() {
    log "Moving temporary files from rpc-translator-138..."
    
    if [ ! -d "rpc-translator-138" ]; then
        warn "rpc-translator-138 directory not found"
        return
    fi
    
    local temp_patterns=(
        "FIX_*.md"
        "QUICK_FIX*.md"
        "RUN_NOW.md"
        "EXECUTE_NOW.md"
        "EXECUTION_READY.md"
        "LOAD_KEYS_NOW.md"
        "FIX_PERMISSIONS*.md"
        "*COMPLETE*.md"
        "*FINAL*.md"
        "*STATUS*.md"
    )
    
    for pattern in "${temp_patterns[@]}"; do
        find rpc-translator-138 -maxdepth 1 -name "$pattern" -type f | while read -r file; do
            filename=$(basename "$file")
            # Skip README and important docs
            if [[ "$filename" != "README.md" ]] && [[ ! "$filename" =~ ^(DEPLOYMENT|API_METHODS|QUICK_REFERENCE|QUICK_START|QUICK_SETUP) ]]; then
                move_file "$file" "rpc-translator-138/docs/archive/$filename" "Temporary fix/status file"
            fi
        done
    done
}

# Move completion/migration status files from docs
move_docs_status_files() {
    log "Moving status files from docs directory..."
    
    if [ ! -d "docs" ]; then
        warn "docs directory not found"
        return
    fi
    
    find docs -maxdepth 1 -name "*COMPLETE*.md" -o -name "*FINAL*.md" -o -name "*MIGRATION*.md" | while read -r file; do
        filename=$(basename "$file")
        # Skip if it's actual documentation
        if [[ ! "$filename" =~ ^(DOCUMENTATION|CONTRIBUTOR|STYLE|GUIDE|README) ]]; then
            move_file "$file" "reports/$filename" "Status file in docs directory"
        fi
    done
}

# Consolidate duplicate status files (keep most recent)
consolidate_duplicates() {
    log "Identifying duplicate status files..."
    
    # This is a placeholder - actual consolidation requires content comparison
    # For now, we'll just log potential duplicates
    local status_files=(
        "ALL_TASKS_COMPLETE.md"
        "ALL_TASKS_COMPLETE_FINAL.md"
        "ALL_STEPS_COMPLETE.md"
        "ALL_NEXT_STEPS_COMPLETE.md"
    )
    
    for file in "${status_files[@]}"; do
        if [ -f "$file" ]; then
            warn "Potential duplicate: $file (consider consolidating)"
        fi
    done
}

# Main execution
main() {
    log "========================================="
    log "Markdown Files Cleanup Script"
    log "========================================="
    log "Project Root: $PROJECT_ROOT"
    log "Dry Run: $DRY_RUN"
    log "Log File: $LOG_FILE"
    log ""
    
    if [ "$DRY_RUN" = "true" ]; then
        warn "DRY RUN MODE - No files will be moved"
        warn "Set DRY_RUN=false to actually move files"
        log ""
    fi
    
    create_directories
    move_timestamped_inventories
    move_status_reports
    move_vmid_reports
    move_network_reports
    move_service_reports
    move_rpc_translator_temp_files
    move_docs_status_files
    consolidate_duplicates
    
    log ""
    log "========================================="
    log "Cleanup complete!"
    log "========================================="
    
    if [ "$DRY_RUN" = "true" ]; then
        log ""
        log "Review the log above, then run with:"
        log "  DRY_RUN=false $0"
    fi
}

main "$@"
