#!/usr/bin/env perl
use strict;
use warnings;
no warnings 'once';
use Test::More;
use File::Basename;
use POSIX;

# Mocking essential subroutines and globals from mysqltuner.pl
our %myvar;
our %mystat;
our %result;
our %opt = ( 'max-password-checks' => 100 );
our $is_win = 0;

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

{
    no warnings 'redefine';
    *main::infoprint = sub {};
    *main::goodprint = sub {};
    *main::badprint = sub {};
    *main::debugprint = sub {};
    *main::get_transport_prefix = sub { return ''; };
}

# Test Case 1: MySQL 9.6 Replication Commands
%myvar = ( 'version' => '9.6.0' );
%mystat = ();

# This part checks if the variables are set correctly in the script
# We can't easily call the anonymous blocks or main flow, but we can test the version detection logic directly.

note "Testing MySQL 9.x version detection and command routing";

my $is_mysql8_plus =
  ( $myvar{'version'} =~ /^[89]\./ && $myvar{'version'} !~ /mariadb/i )
  || ( $myvar{'version'} =~ /^[0-9]{2,}\./ && $myvar{'version'} !~ /mariadb/i );

ok($is_mysql8_plus, "MySQL 9.6 detected as 8+ (modern commands enabled)");

$myvar{'version'} = '8.4.0';
$is_mysql8_plus =
  ( $myvar{'version'} =~ /^[89]\./ && $myvar{'version'} !~ /mariadb/i )
  || ( $myvar{'version'} =~ /^[0-9]{2,}\./ && $myvar{'version'} !~ /mariadb/i );
ok($is_mysql8_plus, "MySQL 8.4 detected as 8+");

$myvar{'version'} = '10.11.5-MariaDB';
$is_mysql8_plus =
  ( $myvar{'version'} =~ /^[89]\./ && $myvar{'version'} !~ /mariadb/i )
  || ( $myvar{'version'} =~ /^[0-9]{2,}\./ && $myvar{'version'} !~ /mariadb/i );
ok(!$is_mysql8_plus, "MariaDB not detected as MySQL 8+");

# Test Case 2: InnoDB Log Metrics Safeguards
note "Testing InnoDB Log Metrics Safeguards";
%mystat = (
    'Innodb_buffer_pool_read_requests' => 1000,
    'Innodb_buffer_pool_reads' => 10,
);
# Innodb_log_writes and Innodb_log_write_requests are missing
# The script should not crash or warn when calling mysql_innodb (if we could isolate it)
# We'll check the logic we implemented

ok(!defined $mystat{'Innodb_log_writes'}, "Innodb_log_writes missing as expected");
ok(!defined $mystat{'Innodb_log_write_requests'}, "Innodb_log_write_requests missing as expected");

# Test Case 3: Storage Detection Logic (Infrastructure-Aware)
note "Testing detect_infrastructure (local mock)";
# We'll mock glob and open to test detect_infrastructure
{
    no warnings 'redefine';
    *CORE::GLOBAL::glob = sub { return ('/sys/block/sda'); };
}

# detect_infrastructure uses /sys/block/sda/queue/rotational
# We can't easily mock file system for CORE::open in a require
# But we can verify the structure and architecture detection

my $arch = ( POSIX::uname() )[4];
my $infra = main::detect_infrastructure();
is($infra->{'architecture'}, $arch, "Architecture detection works ($arch)");

done_testing();
