#!/bin/bash

# Test script pentru debugging autoSMART collector
# Acest script permite testarea manualelor cu debugging activ

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

echo "🔍 Testing autoSMART collector debugging..."
echo "Project root: $PROJECT_ROOT"

# Verifică dacă există configurația de debug
if [[ -f "/etc/default/autosmart" ]]; then
    echo "✓ Found configuration file: /etc/default/autosmart"
    echo "Current configuration:"
    cat /etc/default/autosmart
    echo ""
else
    echo "❌ Configuration file /etc/default/autosmart not found"
    echo "Creating test configuration..."
    sudo mkdir -p /etc/default
    sudo tee /etc/default/autosmart > /dev/null << 'EOF'
# AutoSMART Configuration - Test Debug Mode
AUTOSMART_DEBUG="true"
EOF
    echo "✓ Test configuration created"
fi

# Verifică dacă există fișierul de configurare pentru daemon
CONFIG_FILE="$PROJECT_ROOT/test-config.yaml"
if [[ ! -f "$CONFIG_FILE" ]]; then
    echo "Creating test configuration file: $CONFIG_FILE"
    cat > "$CONFIG_FILE" << 'EOF'
node:
  id: test-node
  scan_interval: 30
  store_unchanged: false
  
collection:
  full_scan_interval: 300
  
database:
  host: 192.168.2.102
  database: autosmart
  user: autosmart
  password: autosmart123
EOF
    echo "✓ Test config created: $CONFIG_FILE"
fi

echo ""
echo "🔧 Available devices for testing:"
ls -la /dev/sd* /dev/nvme* 2>/dev/null | head -10

echo ""
echo "💾 Testing database connectivity..."
if command -v psql >/dev/null; then
    echo "Testing connection to database..."
    psql -h 192.168.2.102 -U autosmart -d autosmart -c "SELECT 'Database connection OK' as status;" 2>/dev/null || echo "❌ Database connection failed"
else
    echo "❌ psql not available for testing"
fi

echo ""
echo "🚀 To run collector in debug mode:"
echo "export AUTOSMART_DEBUG=true"
echo "sudo -E perl $SCRIPT_DIR/smart-collector-daemon.pl --config $CONFIG_FILE --debug --foreground"

echo ""
echo "📊 To check hdd_presence table:"
echo "psql -h 192.168.2.102 -U autosmart -d autosmart -c \"SELECT * FROM hdd_presence;\""

echo ""
echo "📋 To check hdd_inventory table:"
echo "psql -h 192.168.2.102 -U autosmart -d autosmart -c \"SELECT id, serial_number, model_name, current_node_id, last_seen FROM hdd_inventory;\""

echo ""
echo "🔍 To check SMART readings:"
echo "psql -h 192.168.2.102 -U autosmart -d autosmart -c \"SELECT COUNT(*) as total_readings FROM smart_readings;\""
