use strict;
use warnings;
no warnings 'once';
use Test::More;

require './tests/MySQLTuner/TestHelper.pm';

# Mocking variables and functions from mysqltuner.pl
our %myvar;
our %mystat;
our %mycalc;
our @adjvars;
our @generalrec;
our @infoprints;
our @goodprints;
our @badprints;

sub infoprint { push @infoprints, $_[0]; }
sub goodprint { push @goodprints, $_[0]; }
sub badprint  { push @badprints, $_[0]; }
sub subheaderprint { }

sub mysql_version_ge {
    my ($major, $minor, $patch) = @_;
    # Mocking version checks if needed
    return 1;
}

# Simplified/Mocked implementation of what we WANT to add to mysqltuner.pl
sub mock_mysql_innodb_isolation {
    # 1. Isolation Levels
    my $isolation = $myvar{'transaction_isolation'} || $myvar{'tx_isolation'} || $myvar{'isolation_level'};
    if (defined $isolation) {
        infoprint "Transaction Isolation Level: $isolation";
    }

    # 2. innodb_snapshot_isolation (MariaDB)
    if (defined $myvar{'innodb_snapshot_isolation'}) {
        infoprint "InnoDB Snapshot Isolation: " . $myvar{'innodb_snapshot_isolation'};
        if ($myvar{'innodb_snapshot_isolation'} eq 'OFF' && $isolation eq 'REPEATABLE-READ') {
            badprint "innodb_snapshot_isolation is OFF with REPEATABLE-READ (Stricter snapshot isolation is disabled)";
        }
    }

    # 3. Transaction Metrics
    if (defined $mycalc{'innodb_active_transactions'}) {
         infoprint "Active InnoDB Transactions: " . $mycalc{'innodb_active_transactions'};
    }
    if (defined $mycalc{'innodb_longest_transaction_duration'}) {
         infoprint "Longest InnoDB Transaction Duration: " . $mycalc{'innodb_longest_transaction_duration'} . "s";
         if ($mycalc{'innodb_longest_transaction_duration'} > 3600) {
             badprint "Long running InnoDB transaction detected (> 1 hour)";
         }
    }
}

# Test Case 1: Standard REPEATABLE-READ
%myvar = (
    transaction_isolation => 'REPEATABLE-READ',
    innodb_snapshot_isolation => 'ON'
);
%mycalc = (
    innodb_active_transactions => 5,
    innodb_longest_transaction_duration => 120
);
@infoprints = (); @goodprints = (); @badprints = ();
mock_mysql_innodb_isolation();
ok(grep(/Transaction Isolation Level: REPEATABLE-READ/, @infoprints), "Detected transaction_isolation");
ok(grep(/InnoDB Snapshot Isolation: ON/, @infoprints), "Detected innodb_snapshot_isolation");
ok(grep(/Active InnoDB Transactions: 5/, @infoprints), "Detected active transactions");

# Test Case 2: MariaDB with snapshot isolation OFF
%myvar = (
    tx_isolation => 'REPEATABLE-READ',
    innodb_snapshot_isolation => 'OFF'
);
@infoprints = (); @goodprints = (); @badprints = ();
mock_mysql_innodb_isolation();
ok(grep(/Transaction Isolation Level: REPEATABLE-READ/, @infoprints), "Detected tx_isolation");
ok(grep(/innodb_snapshot_isolation is OFF/, @badprints), "Warned about snapshot isolation OFF");

# Test Case 3: Long running transaction
%mycalc = (
    innodb_longest_transaction_duration => 5000
);
@infoprints = (); @goodprints = (); @badprints = ();
mock_mysql_innodb_isolation();
ok(grep(/Long running InnoDB transaction detected/, @badprints), "Warned about long running transaction");

done_testing();
