#!/usr/bin/perl

=head1 NAME

test-smart-collection.pl - Simple SMART data collection test

=head1 DESCRIPTION

Simplified SMART data collection test for autoSMART deployment verification.

=cut

use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/../lib";

use SmartCollector;
use DBI;
use JSON::XS;

# Configuration from environment
my $config = {
    db_host => $ENV{AUTOSMART_DB_HOST} || '192.168.2.102',
    db_port => $ENV{AUTOSMART_DB_PORT} || '5432',
    db_name => $ENV{AUTOSMART_DB_NAME} || 'autosmart',
    db_user => $ENV{AUTOSMART_DB_USER} || 'autosmart',
    db_pass => $ENV{AUTOSMART_DB_PASS} || 'autoSMART2025!',
    node_id => $ENV{AUTOSMART_NODE_ID} || 'ebony',
    debug => $ENV{AUTOSMART_DEBUG} || 2,
};

print "=== autoSMART SMART Collection Test ===\n\n";

# Test database connection
print "Testing database connection...\n";
my $dsn = "DBI:Pg:dbname=$config->{db_name};host=$config->{db_host};port=$config->{db_port}";
my $dbh = DBI->connect($dsn, $config->{db_user}, $config->{db_pass}, {
    RaiseError => 1,
    AutoCommit => 1,
    PrintError => 0
}) or die "Failed to connect to database: $DBI::errstr\n";

print "✓ Database connection successful\n\n";

# Initialize collector
print "Initializing SMART collector...\n";
my $collector = SmartCollector->new($config);
print "✓ Collector initialized\n\n";

# Discover available drives
print "Discovering storage devices...\n";
my @devices = glob('/dev/sd[a-z]');

for my $device (@devices) {
    print "Found device: $device\n";
    
    # Test SMART data collection
    print "  Collecting SMART data...\n";
    my $smart_data = $collector->collect_smart_data($device);
    
    if ($smart_data) {
        print "  ✓ SMART data collected successfully\n";
        print "    Serial: $smart_data->{serial_number}\n";
        print "    Model: $smart_data->{model_name}\n";
        print "    Temperature: $smart_data->{temperature}°C\n";
        print "    Parameters: " . scalar(keys %{$smart_data->{parameters}}) . "\n";
        
        # Create drive info structure
        my $drive_info = {
            device_path => $device,
            serial_number => $smart_data->{serial_number},
            model_name => $smart_data->{model_name}
        };
        
        # Store in database
        print "  Storing in database...\n";
        if ($collector->store_smart_data($drive_info, $smart_data)) {
            print "  ✓ Data stored successfully\n";
        } else {
            print "  ✗ Failed to store data\n";
        }
        
    } else {
        print "  ✗ Failed to collect SMART data\n";
    }
    print "\n";
}

# Check database contents
print "Checking database contents:\n";

my $sth = $dbh->prepare("SELECT COUNT(*) FROM hdd_inventory");
$sth->execute();
my ($hdd_count) = $sth->fetchrow_array();
print "  HDD Inventory: $hdd_count drives\n";

$sth = $dbh->prepare("SELECT COUNT(*) FROM smart_readings");
$sth->execute();
my ($reading_count) = $sth->fetchrow_array();
print "  SMART Readings: $reading_count readings\n";

$sth = $dbh->prepare("SELECT COUNT(*) FROM hdd_migrations");
$sth->execute();
my ($migration_count) = $sth->fetchrow_array();
print "  HDD Migrations: $migration_count migrations\n";

# Show recent readings
if ($reading_count > 0) {
    print "\nRecent SMART readings:\n";
    $sth = $dbh->prepare(q{
        SELECT hi.serial_number, hi.model_name, sr.timestamp, sr.temperature, sr.reading_type
        FROM smart_readings sr
        JOIN hdd_inventory hi ON sr.hdd_id = hi.id
        ORDER BY sr.timestamp DESC
        LIMIT 5
    });
    $sth->execute();
    
    while (my $row = $sth->fetchrow_hashref()) {
        printf "  %s (%s) - %s - %d°C - %s\n",
               $row->{serial_number},
               $row->{model_name},
               $row->{timestamp},
               $row->{temperature} || 0,
               $row->{reading_type};
    }
}

$dbh->disconnect();

print "\n=== Test Complete ===\n";
