#!/usr/bin/env bash
# Cleanup Old, Backup, and Unreferenced Files
# Safely removes old files, backups, and unused files from both local and remote
#
# This script identifies and removes:
# - Backup directories (backup-*, *backup*)
# - Temporary files (*.tmp, *.temp, *~, *.swp)
# - Old log files (logs/*.log older than 30 days)
# - Duplicate/unused files
# - Old documentation that's been superseded

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}"
CLEAN_LOCAL="${CLEAN_LOCAL:-true}"
CLEAN_REMOTE="${CLEAN_REMOTE:-true}"
MIN_LOG_AGE_DAYS=30

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

Cleanup old, backup, and unreferenced files from project directories.

Options:
  --execute        Actually delete files (default: dry-run, only shows what would be deleted)
  --skip-remote    Skip cleaning remote host (ml110)
  --skip-local     Skip cleaning local project
  --help           Show this help

Safety:
  - By default, runs in DRY-RUN mode (shows files but doesn't delete)
  - Use --execute to actually delete files
  - Creates a manifest of files that will be deleted
EOF
            exit 0
            ;;
        *)
            log_error "Unknown option: $1"
            exit 1
            ;;
    esac
done

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

log_info "========================================="
log_info "File Cleanup Script"
log_info "========================================="
log_info "Mode: $([ "$DRY_RUN" == "true" ] && echo "DRY-RUN (no files will be deleted)" || echo "EXECUTE (files will be deleted)")"
log_info "Manifest: $CLEANUP_MANIFEST"
log_info ""

# Function to find and catalog files to delete
find_cleanup_targets() {
    local base_dir="$1"
    local label="$2"
    
    log_info "=== Scanning $label ==="
    local count=0
    
    # Backup directories
    while IFS= read -r dir; do
        if [[ -d "$dir" ]]; then
            echo "$dir" >> "$CLEANUP_MANIFEST"
            echo "DIR: $dir"
            ((count++))
        fi
    done < <(find "$base_dir" -type d -name "*backup*" 2>/dev/null)
    
    # Temporary directories
    while IFS= read -r dir; do
        if [[ -d "$dir" ]] && [[ "$dir" != "$base_dir" ]]; then
            echo "$dir" >> "$CLEANUP_MANIFEST"
            echo "DIR: $dir"
            ((count++))
        fi
    done < <(find "$base_dir" -type d \( -name "*tmp*" -o -name "*temp*" \) 2>/dev/null)
    
    # Temporary/backup files
    while IFS= read -r file; do
        if [[ -f "$file" ]]; then
            echo "$file" >> "$CLEANUP_MANIFEST"
            echo "FILE: $file"
            ((count++))
        fi
    done < <(find "$base_dir" -type f \( -name "*.bak" -o -name "*.old" -o -name "*~" -o -name "*.swp" -o -name "*.tmp" -o -name "*.temp" \) 2>/dev/null)
    
    # Old log files (older than MIN_LOG_AGE_DAYS)
    if [[ -d "$base_dir/logs" ]]; then
        while IFS= read -r file; do
            if [[ -f "$file" ]]; then
                local file_age=$(( ($(date +%s) - $(stat -c %Y "$file" 2>/dev/null || echo 0)) / 86400 ))
                if [[ $file_age -gt $MIN_LOG_AGE_DAYS ]]; then
                    echo "$file" >> "$CLEANUP_MANIFEST"
                    echo "OLD LOG ($file_age days): $file"
                    ((count++))
                fi
            fi
        done < <(find "$base_dir/logs" -type f -name "*.log" 2>/dev/null)
    fi
    
    # temp-all-keys-* directories in smom-dbis-138
    if [[ "$base_dir" == *"smom-dbis-138"* ]]; then
        while IFS= read -r dir; do
            if [[ -d "$dir" ]]; then
                echo "$dir" >> "$CLEANUP_MANIFEST"
                echo "TEMP KEY GEN: $dir"
                ((count++))
            fi
        done < <(find "$base_dir" -type d -name "temp-all-keys-*" 2>/dev/null)
    fi
    
    log_info "Found $count items to clean"
    echo "$count"
}

# Function to delete files from manifest
delete_from_manifest() {
    local manifest_file="$1"
    local label="$2"
    
    if [[ ! -f "$manifest_file" ]]; then
        log_warn "Manifest file not found: $manifest_file"
        return 0
    fi
    
    local count=$(wc -l < "$manifest_file" | tr -d ' ')
    if [[ $count -eq 0 ]]; then
        log_info "No files to delete for $label"
        return 0
    fi
    
    log_info "Deleting $count items from $label..."
    local deleted=0
    local failed=0
    
    while IFS= read -r target; do
        if [[ -z "$target" ]]; then
            continue
        fi
        
        if [[ -e "$target" ]]; then
            if rm -rf "$target" 2>/dev/null; then
                ((deleted++))
            else
                log_warn "Failed to delete: $target"
                ((failed++))
            fi
        fi
    done < "$manifest_file"
    
    log_success "Deleted $deleted items, $failed failures"
}

# Clean local project
if [[ "$CLEAN_LOCAL" == "true" ]]; then
    log_info ""
    log_info "=== Local Project Cleanup ==="
    
    # Clean proxmox project
    PROXMOX_CLEANUP="$PROJECT_ROOT/logs/proxmox-cleanup-$(date +%Y%m%d-%H%M%S).txt"
    > "$PROXMOX_CLEANUP"
    
    find_cleanup_targets "$PROJECT_ROOT" "proxmox project" | tee -a "$PROXMOX_CLEANUP" | tail -20
    
    proxmox_count=$(tail -1 "$PROXMOX_CLEANUP" | grep -oE '[0-9]+' | head -1 || echo "0")
    
    # Clean smom-dbis-138 project
    if [[ -d "$PROJECT_ROOT/../smom-dbis-138" ]]; then
        SMOM_CLEANUP="$PROJECT_ROOT/logs/smom-cleanup-$(date +%Y%m%d-%H%M%S).txt"
        > "$SMOM_CLEANUP"
        
        find_cleanup_targets "$PROJECT_ROOT/../smom-dbis-138" "smom-dbis-138 project" | tee -a "$SMOM_CLEANUP" | tail -20
        
        smom_count=$(tail -1 "$SMOM_CLEANUP" | grep -oE '[0-9]+' | head -1 || echo "0")
    else
        smom_count=0
    fi
    
    total_local=$((proxmox_count + smom_count))
    
    if [[ "$DRY_RUN" != "true" ]] && [[ $total_local -gt 0 ]]; then
        log_info ""
        log_warn "Executing deletion of $total_local local items..."
        delete_from_manifest "$CLEANUP_MANIFEST" "local project"
    fi
fi

# Clean remote host
if [[ "$CLEAN_REMOTE" == "true" ]]; then
    log_info ""
    log_info "=== Remote Host Cleanup (ml110) ==="
    
    # Test SSH connection
    if ! sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 \
        "${REMOTE_USER}@${REMOTE_HOST}" "echo 'Connected'" 2>/dev/null; then
        log_warn "Cannot connect to ${REMOTE_HOST}, skipping remote cleanup"
    else
        log_info "Scanning remote host..."
        
        # Get list of files to clean on remote
        REMOTE_CLEANUP_LIST=$(sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no \
            "${REMOTE_USER}@${REMOTE_HOST}" "cd /opt && \
            find smom-dbis-138* -type d -name '*backup*' 2>/dev/null && \
            find smom-dbis-138* -type d \( -name '*tmp*' -o -name '*temp*' \) 2>/dev/null && \
            find smom-dbis-138* -type f \( -name '*.bak' -o -name '*.old' -o -name '*~' -o -name '*.swp' \) 2>/dev/null" 2>/dev/null | head -50)
        
        remote_count=0
        if [[ -n "$REMOTE_CLEANUP_LIST" ]]; then
            echo "$REMOTE_CLEANUP_LIST" | while IFS= read -r item; do
                if [[ -n "$item" ]]; then
                    echo "/opt/$item" >> "$CLEANUP_MANIFEST"
                    echo "REMOTE: /opt/$item"
                    ((remote_count++))
                fi
            done
            
            log_info "Found $remote_count items to clean on remote"
        else
            log_info "No cleanup targets found on remote"
        fi
        
        if [[ "$DRY_RUN" != "true" ]] && [[ $remote_count -gt 0 ]]; then
            log_info ""
            log_warn "Executing deletion of $remote_count remote items..."
            
            # Delete remote files
            sshpass -p 'L@kers2010' ssh -o StrictHostKeyChecking=no \
                "${REMOTE_USER}@${REMOTE_HOST}" "cd /opt && \
                find smom-dbis-138* -type d -name '*backup*' -exec rm -rf {} + 2>/dev/null; \
                find smom-dbis-138* -type d \( -name '*tmp*' -o -name '*temp*' \) -exec rm -rf {} + 2>/dev/null; \
                find smom-dbis-138* -type f \( -name '*.bak' -o -name '*.old' -o -name '*~' -o -name '*.swp' \) -delete 2>/dev/null; \
                echo 'Remote cleanup completed'"
            
            log_success "Remote cleanup completed"
        fi
    fi
fi

# Summary
log_info ""
log_info "========================================="
log_info "Cleanup Summary"
log_info "========================================="
log_info "Manifest file: $CLEANUP_MANIFEST"
log_info "Mode: $([ "$DRY_RUN" == "true" ] && echo "DRY-RUN" || echo "EXECUTED")"
log_info ""

if [[ "$DRY_RUN" == "true" ]]; then
    log_warn "This was a DRY-RUN. No files were deleted."
    log_info "Review the manifest file and run with --execute to delete files:"
    log_info "  $0 --execute"
else
    log_success "Cleanup completed. Check manifest for details: $CLEANUP_MANIFEST"
fi

log_info ""

