#!/bin/bash
# Consolidate Duplicate Status Files
# Archives older duplicates, keeps most recent/complete version

set -euo pipefail

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

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

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

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

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

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

log "Consolidating duplicate status files..."
log "Archive directory: $ARCHIVE_DIR"
log ""

# Function to keep newest file and archive others
consolidate_group() {
    local base_name="$1"
    shift
    local files=("$@")
    
    if [ ${#files[@]} -le 1 ]; then
        return
    fi
    
    log "Consolidating: $base_name (${#files[@]} files)"
    
    # Find newest file (by modification time)
    local newest_file=""
    local newest_time=0
    
    for file in "${files[@]}"; do
        if [ ! -f "$file" ]; then
            continue
        fi
        
        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
        warn "No valid files found for $base_name"
        return
    fi
    
    success "Keeping: $newest_file"
    
    # Archive others
    for file in "${files[@]}"; do
        if [ "$file" != "$newest_file" ] && [ -f "$file" ]; then
            local filename=$(basename "$file")
            local dest="$ARCHIVE_DIR/$filename"
            
            # Handle duplicates in archive
            local counter=1
            while [ -f "$dest" ]; do
                dest="$ARCHIVE_DIR/${filename%.md}_${counter}.md"
                counter=$((counter + 1))
            done
            
            mv "$file" "$dest"
            success "  Archived: $file → $dest"
        fi
    done
    log ""
}

# Consolidate reports/status/ duplicates
log "=== Consolidating reports/status/ ==="

# BESU_RPC files
consolidate_group "BESU_RPC" \
    "reports/status/BESU_RPC_COMPLETE_CHECK.md" \
    "reports/status/BESU_RPC_STATUS_CHECK.md" \
    "reports/status/BESU_RPC_STATUS_FINAL.md"

# R630_02_MINOR_ISSUES files
consolidate_group "R630_02_MINOR_ISSUES" \
    "reports/status/R630_02_MINOR_ISSUES_COMPLETE.md" \
    "reports/status/R630_02_MINOR_ISSUES_FINAL.md"

# DBIS_SERVICES files
consolidate_group "DBIS_SERVICES" \
    "reports/status/DBIS_SERVICES_STATUS_FINAL.md" \
    "reports/status/DBIS_SERVICES_STATUS_CHECK.md" \
    "reports/status/DBIS_SERVICES_STATUS_REPORT.md"

# BLOCKSCOUT_START files
consolidate_group "BLOCKSCOUT_START" \
    "reports/status/BLOCKSCOUT_START_COMPLETE.md" \
    "reports/status/BLOCKSCOUT_START_STATUS.md"

# PHASE1_IP_INVESTIGATION files
consolidate_group "PHASE1_IP_INVESTIGATION" \
    "reports/status/PHASE1_IP_INVESTIGATION_STATUS.md" \
    "reports/status/PHASE1_IP_INVESTIGATION_COMPLETE.md"

# BLOCK_PRODUCTION files
consolidate_group "BLOCK_PRODUCTION" \
    "reports/status/BLOCK_PRODUCTION_STATUS.md"

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

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

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