#!/usr/bin/env bash
# Review ml110 for duplicates, missing files, and gaps
# Comprehensive review of ml110 structure and completeness

set -euo pipefail

REMOTE_HOST="192.168.11.10"
REMOTE_USER="root"
REMOTE_PASS="L@kers2010"
REMOTE_BASE="/opt"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
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"; }
log_section() { echo -e "${CYAN}=== $1 ===${NC}"; }

log_section "ml110 Completeness Review"
echo "Remote: ${REMOTE_USER}@${REMOTE_HOST}"
echo ""

# Test connection
if ! sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 \
    "${REMOTE_USER}@${REMOTE_HOST}" "echo 'Connected'" 2>/dev/null; then
    log_error "Cannot connect to ${REMOTE_HOST}"
    exit 1
fi

log_success "Connected to ${REMOTE_HOST}"
echo ""

# 1. Check for duplicate directories
log_section "1. Duplicate Directories Check"

DUPLICATE_DIRS=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
    "${REMOTE_USER}@${REMOTE_HOST}" "cd ${REMOTE_BASE} && find . -maxdepth 1 -type d -name 'smom-dbis-138*' 2>/dev/null" 2>/dev/null)

if echo "$DUPLICATE_DIRS" | wc -l | grep -q "2"; then
    log_success "Expected directories found (smom-dbis-138 and smom-dbis-138-proxmox)"
else
    DUPLICATE_COUNT=$(echo "$DUPLICATE_DIRS" | wc -l)
    if [[ $DUPLICATE_COUNT -gt 2 ]]; then
        log_warn "Found $DUPLICATE_COUNT smom-dbis-138* directories (expected 2)"
        echo "$DUPLICATE_DIRS"
    else
        log_info "Directories: $DUPLICATE_DIRS"
    fi
fi

echo ""

# 2. Check for duplicate files
log_section "2. Duplicate Files Check"

# Check for files with same name in different locations
DUPLICATE_FILES=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
    "${REMOTE_USER}@${REMOTE_HOST}" "cd ${REMOTE_BASE} && find smom-dbis-138* -type f -name '*.sh' -o -name '*.conf' -o -name '*.md' 2>/dev/null | xargs -I {} basename {} | sort | uniq -d" 2>/dev/null)

if [[ -n "$DUPLICATE_FILES" ]]; then
    log_warn "Found files with duplicate names:"
    echo "$DUPLICATE_FILES" | head -10 | sed 's/^/  /'
else
    log_success "No duplicate file names found"
fi

echo ""

# 3. Missing Critical Files
log_section "3. Missing Critical Files Check"

CRITICAL_FILES=(
    "smom-dbis-138-proxmox/config/proxmox.conf"
    "smom-dbis-138-proxmox/config/network.conf"
    "smom-dbis-138-proxmox/scripts/deployment/deploy-validated-set.sh"
    "smom-dbis-138-proxmox/scripts/copy-besu-config.sh"
    "smom-dbis-138-proxmox/scripts/fix-container-ips.sh"
    "smom-dbis-138-proxmox/scripts/network/bootstrap-network.sh"
    "smom-dbis-138/config/genesis.json"
)

MISSING_COUNT=0
for file in "${CRITICAL_FILES[@]}"; do
    EXISTS=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
        "${REMOTE_USER}@${REMOTE_HOST}" "cd ${REMOTE_BASE} && [ -f \"$file\" ] && echo 'yes' || echo 'no'" 2>/dev/null)
    
    if [[ "$EXISTS" == "yes" ]]; then
        log_success "$(basename "$file")"
    else
        log_error "Missing: $file"
        MISSING_COUNT=$((MISSING_COUNT + 1))
    fi
done

echo ""

# 4. Validator Keys Check
log_section "4. Validator Keys Completeness"

VALIDATOR_KEYS=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
    "${REMOTE_USER}@${REMOTE_HOST}" "cd ${REMOTE_BASE} && if [ -d 'smom-dbis-138/keys/validators' ]; then find smom-dbis-138/keys/validators -type d -name 'validator-*' 2>/dev/null | sort -V; else echo 'DIR_NOT_FOUND'; fi" 2>/dev/null)

if [[ "$VALIDATOR_KEYS" == "DIR_NOT_FOUND" ]]; then
    log_error "keys/validators directory not found"
else
    KEY_COUNT=$(echo "$VALIDATOR_KEYS" | grep -c "validator-" || echo "0")
    log_info "Found $KEY_COUNT validator key directories"
    
    for i in 1 2 3 4 5; do
        EXISTS=$(echo "$VALIDATOR_KEYS" | grep -q "validator-$i" && echo "yes" || echo "no")
        if [[ "$EXISTS" == "yes" ]]; then
            log_success "validator-$i"
        else
            log_error "Missing: validator-$i"
        fi
    done
fi

echo ""

# 5. Directory Structure Check
log_section "5. Directory Structure Check"

EXPECTED_DIRS=(
    "smom-dbis-138-proxmox/config"
    "smom-dbis-138-proxmox/scripts"
    "smom-dbis-138-proxmox/scripts/deployment"
    "smom-dbis-138-proxmox/scripts/network"
    "smom-dbis-138-proxmox/scripts/validation"
    "smom-dbis-138/config"
    "smom-dbis-138/keys"
    "smom-dbis-138/keys/validators"
)

for dir in "${EXPECTED_DIRS[@]}"; do
    EXISTS=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
        "${REMOTE_USER}@${REMOTE_HOST}" "cd ${REMOTE_BASE} && [ -d \"$dir\" ] && echo 'yes' || echo 'no'" 2>/dev/null)
    
    if [[ "$EXISTS" == "yes" ]]; then
        log_success "$dir/"
    else
        log_error "Missing directory: $dir/"
    fi
done

echo ""

# 6. File Count Comparison
log_section "6. File Counts"

CONFIG_COUNT=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
    "${REMOTE_USER}@${REMOTE_HOST}" "cd ${REMOTE_BASE} && find smom-dbis-138-proxmox/config -name '*.conf' -type f 2>/dev/null | wc -l" 2>/dev/null)

SCRIPT_COUNT=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
    "${REMOTE_USER}@${REMOTE_HOST}" "cd ${REMOTE_BASE} && find smom-dbis-138-proxmox/scripts -name '*.sh' -type f 2>/dev/null | wc -l" 2>/dev/null)

DOC_COUNT=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no \
    "${REMOTE_USER}@${REMOTE_HOST}" "cd ${REMOTE_BASE} && find smom-dbis-138-proxmox/docs -name '*.md' -type f 2>/dev/null | wc -l" 2>/dev/null)

log_info "Configuration files: $CONFIG_COUNT"
log_info "Script files: $SCRIPT_COUNT"
log_info "Documentation files: $DOC_COUNT"

echo ""

# Summary
log_section "Summary"

if [[ $MISSING_COUNT -eq 0 ]]; then
    log_success "All critical files present"
else
    log_error "Found $MISSING_COUNT missing critical files"
fi

log_info "Review complete"

