#!/bin/bash
# Comprehensive Duplicate Consolidation
# Consolidates all duplicate status files across the project

set -euo pipefail

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

ARCHIVE_DIR="reports/archive/duplicates/$(date +%Y-%m-%d)"
mkdir -p "$ARCHIVE_DIR"

log() {
    echo -e "\033[0;34m[$(date +'%Y-%m-%d %H:%M:%S')]\033[0m $1"
}

success() {
    echo -e "\033[0;32m[OK]\033[0m $1"
}

# Function to keep newest and archive others
consolidate_files() {
    local group_name="$1"
    shift
    local files=("$@")
    
    if [ ${#files[@]} -le 1 ]; then
        return
    fi
    
    # Filter to only existing files
    local existing_files=()
    for file in "${files[@]}"; do
        if [ -f "$file" ]; then
            existing_files+=("$file")
        fi
    done
    
    if [ ${#existing_files[@]} -le 1 ]; then
        return
    fi
    
    log "Consolidating: $group_name (${#existing_files[@]} files)"
    
    # Find newest file
    local newest_file=""
    local newest_time=0
    
    for file in "${existing_files[@]}"; do
        local mtime=$(stat -c %Y "$file" 2>/dev/null || echo 0)
        if [ "$mtime" -gt "$newest_time" ]; then
            newest_time=$mtime
            newest_file="$file"
        fi
    done
    
    if [ -z "$newest_file" ]; then
        return
    fi
    
    success "Keeping: $newest_file"
    
    # Archive others
    for file in "${existing_files[@]}"; do
        if [ "$file" != "$newest_file" ]; then
            local filename=$(basename "$file")
            local dest="$ARCHIVE_DIR/$filename"
            
            local counter=1
            while [ -f "$dest" ]; do
                dest="$ARCHIVE_DIR/${filename%.md}_${counter}.md"
                counter=$((counter + 1))
            done
            
            mv "$file" "$dest"
            success "  Archived: $file"
        fi
    done
    echo ""
}

log "=== Comprehensive Duplicate Consolidation ==="
log ""

# Consolidate ALL_TASKS files in reports/status/
consolidate_files "ALL_TASKS (reports/status)" \
    "reports/status/ALL_TASKS_COMPLETE_FINAL.md"

# Consolidate ALL_NEXT_STEPS files in reports/status/
consolidate_files "ALL_NEXT_STEPS (reports/status)" \
    "reports/status/ALL_NEXT_STEPS_COMPLETE.md"

# Consolidate FIREFLY duplicates
consolidate_files "FIREFLY" \
    "reports/status/FIREFLY_FIX_COMPLETE.md" \
    "reports/status/FIREFLY_ALL_FIXED_COMPLETE.md" \
    "reports/status/FIREFLY_ALL_ISSUES_FIXED_FINAL.md" \
    "reports/status/FIREFLY_ISSUES_COMPLETE.md" \
    "reports/status/FIREFLY_FINAL_STATUS.md" \
    "reports/status/FIREFLY_ALL_FIXED_FINAL.md" \
    "reports/status/FIREFLY_COMPLETE_FIX_SUMMARY.md" \
    "reports/status/FIREFLY_COMPLETE_FIX_FINAL.md" \
    "reports/status/FIREFLY_ISSUES_ANALYSIS.md" \
    "reports/status/FIREFLY_ALL_ISSUES_FIXED_COMPLETE.md" \
    "reports/status/FIREFLY_ALL_ISSUES_FIXED.md"

# Consolidate DBIS duplicates
consolidate_files "DBIS_ALL_ISSUES" \
    "reports/status/DBIS_ALL_ISSUES_FIXED_SUMMARY.md" \
    "reports/status/DBIS_ALL_ISSUES_FIXED_FINAL.md" \
    "reports/status/DBIS_ALL_ISSUES_FIXED.md"

consolidate_files "DBIS_SOURCE_CODE_FIXES" \
    "reports/status/DBIS_SOURCE_CODE_FIXES_COMPLETE.md" \
    "reports/status/DBIS_SOURCE_CODE_FIXES_FINAL.md" \
    "reports/status/DBIS_SOURCE_CODE_FIXES_SUCCESS.md" \
    "reports/status/DBIS_SOURCE_CODE_FIXES_APPLIED.md"

consolidate_files "DBIS_COMPLETION" \
    "reports/status/DBIS_COMPLETION_FINAL_SUMMARY.md" \
    "reports/status/DBIS_COMPLETE_STATUS_CHECK_SUMMARY.md"

consolidate_files "DBIS_TASKS" \
    "reports/status/DBIS_TASKS_COMPLETION_STATUS.md" \
    "reports/status/DBIS_TASKS_COMPLETION_REPORT.md"

# Consolidate BESU duplicates
consolidate_files "BESU_FIXES" \
    "reports/status/BESU_FIXES_COMPLETE.md" \
    "reports/status/BESU_ALL_FIXES_COMPLETE.md" \
    "reports/status/BESU_FIXES_APPLIED.md" \
    "reports/status/BESU_FIXES_PROGRESS.md"

consolidate_files "BESU_RPC_FIXES" \
    "reports/status/BESU_RPC_FIXES_FINAL.md" \
    "reports/status/BESU_RPC_FIXES_APPLIED.md"

# Consolidate R630_02 duplicates
consolidate_files "R630_02" \
    "reports/status/R630_02_START_COMPLETE.md" \
    "reports/status/R630_02_NEXT_STEPS_COMPLETE.md" \
    "reports/status/R630_02_SERVICES_FINAL_REPORT.md" \
    "reports/status/R630_02_SERVICES_VERIFICATION_COMPLETE.md"

# Consolidate VMID2400 duplicates
consolidate_files "VMID2400" \
    "reports/status/VMID2400_COMPLETE_STATUS.md" \
    "reports/status/VMID2400_SETUP_COMPLETE.md" \
    "reports/status/VMID2400_NEXT_STEPS_COMPLETE.md" \
    "reports/status/VMID2400_TUNNEL_ROUTING_COMPLETE.md" \
    "reports/status/VMID2400_CONNECTIVITY_FIX_COMPLETE.md" \
    "reports/status/VMID2400_ROUTING_SUMMARY.md"

# Consolidate RESERVED_IP duplicates
consolidate_files "RESERVED_IP" \
    "reports/status/RESERVED_IP_FIX_COMPLETE.md" \
    "reports/status/RESERVED_IP_FIX_COMPLETE_FINAL.md" \
    "reports/status/RESERVED_IP_FIX_SUMMARY.md"

# Consolidate DHCP duplicates
consolidate_files "DHCP_CONVERSION" \
    "reports/status/DHCP_TO_STATIC_CONVERSION_COMPLETE.md" \
    "reports/status/DHCP_TO_STATIC_CONVERSION_FINAL_REPORT.md"

# Consolidate JWT duplicates
consolidate_files "JWT_SETUP" \
    "reports/status/JWT_SETUP_COMPLETE.md" \
    "reports/status/JWT_SETUP_SUMMARY.md"

# Consolidate VALIDATION duplicates
consolidate_files "VALIDATION" \
    "reports/status/VALIDATION_COMPLETE.md" \
    "reports/status/VALIDATION_COMPLETE_SUMMARY.md"

# Consolidate ENHANCEMENTS duplicates
consolidate_files "ENHANCEMENTS" \
    "reports/status/ENHANCEMENTS_COMPLETE.md" \
    "reports/status/ENHANCEMENTS_SUMMARY.md"

# Consolidate COMPLETE duplicates
consolidate_files "COMPLETE_SETUP" \
    "reports/status/COMPLETE_SETUP_SUMMARY.md"

consolidate_files "COMPLETE_EXECUTION" \
    "reports/status/COMPLETE_EXECUTION_SUMMARY.md"

consolidate_files "COMPLETE_IMPLEMENTATION" \
    "reports/status/COMPLETE_IMPLEMENTATION_SUMMARY.md"

log "=== Consolidation Complete ==="
log "Archived files: $(ls -1 "$ARCHIVE_DIR" 2>/dev/null | wc -l)"
log "Archive location: $ARCHIVE_DIR"
