#!/usr/bin/env bash
# Comprehensive Cleanup of Old, Backup, and Unreferenced Files
# Safely removes old files from both local projects and remote ml110
#
# Targets:
# - Backup directories (backup-*, *backup*)
# - Temporary key generation directories (temp-all-keys-*)
# - Old log files (logs/*.log older than 30 days)
# - Temporary files (*.bak, *.old, *~, *.swp)
# - Old documentation files that are no longer referenced

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

# Configuration
DRY_RUN="${DRY_RUN:-true}"
REMOTE_HOST="${REMOTE_HOST:-192.168.11.10}"
REMOTE_USER="${REMOTE_USER:-root}"
REMOTE_PASS="${REMOTE_PASS:-L@kers2010}"
MIN_LOG_AGE_DAYS=30

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

Comprehensive cleanup of old, backup, and unreferenced files.

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

Safety:
  - By default, runs in DRY-RUN mode
  - Use --execute to actually delete files
  - Creates detailed manifest of files to be deleted
EOF
            exit 0
            ;;
        *)
            log_error "Unknown option: $1"
            exit 1
            ;;
    esac
done

# Create cleanup manifest
CLEANUP_LOG="$PROJECT_ROOT/logs/cleanup-$(date +%Y%m%d-%H%M%S).log"
mkdir -p "$PROJECT_ROOT/logs"
> "$CLEANUP_LOG"

log_info "========================================="
log_info "Comprehensive File Cleanup"
log_info "========================================="
log_info "Mode: $([ "$DRY_RUN" == "true" ] && echo "DRY-RUN" || echo "EXECUTE")"
log_info "Log: $CLEANUP_LOG"
log_info ""

TOTAL_FOUND=0
TOTAL_DELETED=0

# Function to safely delete a file/directory
safe_delete() {
    local target="$1"
    local label="${2:-item}"
    
    if [[ ! -e "$target" ]]; then
        return 0
    fi
    
    echo "$target" >> "$CLEANUP_LOG"
    TOTAL_FOUND=$((TOTAL_FOUND + 1))
    
    if [[ "$DRY_RUN" != "true" ]]; then
        if rm -rf "$target" 2>/dev/null; then
            TOTAL_DELETED=$((TOTAL_DELETED + 1))
            echo "✓ Deleted: $target"
            return 0
        else
            echo "✗ Failed: $target" >&2
            return 1
        fi
    else
        echo "Would delete: $target"
        return 0
    fi
}

# Clean local proxmox project
log_info "=== Cleaning Local Proxmox Project ==="
PROXMOX_DIR="$PROJECT_ROOT"

# Old markdown files in root (status/completion docs that are superseded)
OLD_DOCS_PROXMOX=(
    "$PROXMOX_DIR/ACTION_PLAN_NOW.md"
    "$PROXMOX_DIR/DEPLOYMENT_IN_PROGRESS.md"
    "$PROXMOX_DIR/DEPLOYMENT_SOLUTION.md"
    "$PROXMOX_DIR/FINAL_STATUS.txt"
    "$PROXMOX_DIR/IMPLEMENTATION_COMPLETE.md"
    "$PROXMOX_DIR/NEXT_STEPS_QUICK_REFERENCE.md"
    "$PROXMOX_DIR/ORGANIZATION_SUMMARY.md"
    "$PROXMOX_DIR/PROJECT_STRUCTURE.md"
    "$PROXMOX_DIR/QUICK_DEPLOY_FIX.md"
    "$PROXMOX_DIR/QUICK_DEPLOY.md"
    "$PROXMOX_DIR/QUICK_START_VALIDATED_SET.md"
    "$PROXMOX_DIR/STATUS_FINAL.md"
    "$PROXMOX_DIR/STATUS.md"
    "$PROXMOX_DIR/VALIDATED_SET_IMPLEMENTATION_SUMMARY.md"
)

for doc in "${OLD_DOCS_PROXMOX[@]}"; do
    safe_delete "$doc" "old doc"
done

# Temporary besu-enodes directories
while IFS= read -r dir; do
    safe_delete "$dir" "temp enode dir"
done < <(find "$PROXMOX_DIR" -maxdepth 1 -type d -name "besu-enodes-*" 2>/dev/null)

# Old log files in smom-dbis-138-proxmox/logs
if [[ -d "$PROXMOX_DIR/smom-dbis-138-proxmox/logs" ]]; then
    while IFS= read -r logfile; do
        if [[ -f "$logfile" ]]; then
            file_age=$(( ($(date +%s) - $(stat -c %Y "$logfile" 2>/dev/null || echo 0)) / 86400 ))
            if [[ $file_age -gt $MIN_LOG_AGE_DAYS ]]; then
                safe_delete "$logfile" "old log"
            fi
        fi
    done < <(find "$PROXMOX_DIR/smom-dbis-138-proxmox/logs" -type f -name "*.log" 2>/dev/null)
fi

# Backup/temp files (only in specific project directories)
while IFS= read -r file; do
    # Only process files in our project directories
    if [[ "$file" == "$PROXMOX_DIR/"* ]] && [[ "$file" != *"/node_modules/"* ]] && [[ "$file" != *"/ProxmoxVE/"* ]] && [[ "$file" != *"/mcp-proxmox/"* ]] && [[ "$file" != *"/the_order/"* ]]; then
        safe_delete "$file" "backup/temp file"
    fi
done < <(find "$PROXMOX_DIR" -maxdepth 3 -type f \( -name "*.bak" -o -name "*.old" -o -name "*~" -o -name "*.swp" \) 2>/dev/null)

# Clean local smom-dbis-138 project
log_info ""
log_info "=== Cleaning Local smom-dbis-138 Project ==="
# Try different possible locations
SMOM_DIR=""
for possible_dir in "$PROJECT_ROOT/../smom-dbis-138" "/home/intlc/projects/smom-dbis-138"; do
    if [[ -d "$possible_dir" ]]; then
        SMOM_DIR="$possible_dir"
        break
    fi
done

if [[ -n "$SMOM_DIR" ]] && [[ -d "$SMOM_DIR" ]]; then
    log_info "Using smom-dbis-138 directory: $SMOM_DIR"
    # Temporary key generation directories
    while IFS= read -r dir; do
        safe_delete "$dir" "temp key gen dir"
    done < <(find "$SMOM_DIR" -maxdepth 1 -type d -name "temp-all-keys-*" 2>/dev/null)
    
    # Backup key directories (keep only the most recent)
    LATEST_BACKUP=$(find "$SMOM_DIR" -maxdepth 1 -type d -name "backup-keys-*" 2>/dev/null | sort | tail -1)
    while IFS= read -r dir; do
        if [[ "$dir" != "$LATEST_BACKUP" ]]; then
            safe_delete "$dir" "old backup keys"
        fi
    done < <(find "$SMOM_DIR" -maxdepth 1 -type d -name "backup-keys-*" 2>/dev/null)
    
    # Old log files
    if [[ -d "$SMOM_DIR/logs" ]]; then
        while IFS= read -r logfile; do
            if [[ -f "$logfile" ]]; then
                file_age=$(( ($(date +%s) - $(stat -c %Y "$logfile" 2>/dev/null || echo 0)) / 86400 ))
                if [[ $file_age -gt $MIN_LOG_AGE_DAYS ]]; then
                    safe_delete "$logfile" "old log"
                fi
            fi
        done < <(find "$SMOM_DIR/logs" -type f -name "*.log" 2>/dev/null)
    fi
    
    # Temporary/backup files
    while IFS= read -r file; do
        safe_delete "$file" "backup/temp file"
    done < <(find "$SMOM_DIR" -maxdepth 2 -type f \( -name "*.bak" -o -name "*.old" -o -name "*~" -o -name "*.swp" \) ! -path "*/node_modules/*" 2>/dev/null)
else
    log_warn "smom-dbis-138 directory not found: $SMOM_DIR"
fi

# Clean remote ml110
log_info ""
log_info "=== Cleaning Remote Host (ml110) ==="

if sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 \
    "${REMOTE_USER}@${REMOTE_HOST}" "echo 'Connected'" 2>/dev/null; then
    
    log_info "Connected to ${REMOTE_HOST}"
    
    # Get list of files to clean
    REMOTE_CLEANUP=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
        "${REMOTE_USER}@${REMOTE_HOST}" "cd /opt && {
        # Find backup/temp directories
        find smom-dbis-138* -type d -name '*backup*' 2>/dev/null
        find smom-dbis-138* -type d -name 'temp-all-keys-*' 2>/dev/null
        
        # Find old log files (older than $MIN_LOG_AGE_DAYS days)
        find smom-dbis-138*/logs -type f -name '*.log' 2>/dev/null | while read -r log; do
            age=\$(( (\$(date +%s) - \$(stat -c %Y \"\$log\" 2>/dev/null || echo 0)) / 86400 ))
            if [[ \$age -gt $MIN_LOG_AGE_DAYS ]]; then
                echo \"\$log\"
            fi
        done
        
        # Find backup/temp files
        find smom-dbis-138* -type f \( -name '*.bak' -o -name '*.old' -o -name '*~' -o -name '*.swp' \) 2>/dev/null
    }" 2>/dev/null)
    
    if [[ -n "$REMOTE_CLEANUP" ]]; then
        REMOTE_COUNT=0
        echo "$REMOTE_CLEANUP" | while IFS= read -r item; do
            if [[ -n "$item" ]]; then
                REMOTE_COUNT=$((REMOTE_COUNT + 1))
                echo "/opt/$item" >> "$CLEANUP_LOG"
                echo "Would delete (remote): /opt/$item"
                
                if [[ "$DRY_RUN" != "true" ]]; then
                    if sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
                        "${REMOTE_USER}@${REMOTE_HOST}" "rm -rf \"/opt/$item\" 2>/dev/null && echo '✓' || echo '✗'" 2>/dev/null | grep -q "✓"; then
                        TOTAL_DELETED=$((TOTAL_DELETED + 1))
                    fi
                fi
            fi
        done
        
        log_info "Found $REMOTE_COUNT items on remote"
    else
        log_info "No cleanup targets found on remote"
    fi
else
    log_warn "Cannot connect to ${REMOTE_HOST}, skipping remote cleanup"
fi

# Summary
log_info ""
log_info "========================================="
log_info "Cleanup Summary"
log_info "========================================="
log_info "Total items found: $TOTAL_FOUND"

if [[ "$DRY_RUN" == "true" ]]; then
    log_warn "DRY-RUN mode: No files were deleted"
    log_info "Review the log file: $CLEANUP_LOG"
    log_info "Run with --execute to actually delete: $0 --execute"
else
    log_success "Total items deleted: $TOTAL_DELETED"
    log_info "Cleanup log: $CLEANUP_LOG"
fi

log_info ""

