#!/usr/local/cpanel/3rdparty/bin/perl -w
#                                      Copyright 2025 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

# BEGIN Augment generated code block
# Boundary condition validation tests for Random
# Tests extreme probability values and edge cases
# END Augment generated code block

use cPstrict;

use Test2::V0;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use Test2::Tools::Subtest;
use Test2::Tools::Exception;

use Test::MockModule qw/strict/;

use Test::Cpanel::Policy testing => 'Cpanel::Plugins::Components::Rules::Random';

use Cpanel::Plugins::Components::Rules::Random ();

subtest 'Extreme low probability validation' => sub {

    # Test very low probability (should be mostly false)
    my $low_config = {
        rule        => { true => 1 },
        sample_size => 1,
        fleet_size  => 100000           # 0.001% probability
    };

    my $false_count = 0;
    my $total_tests = 1000;

    for my $i ( 1 .. $total_tests ) {
        my $filter = Cpanel::Plugins::Components::Rules::Random->new($low_config);
        $false_count++ unless $filter->is_allowed();
    }

    # Should get mostly false results (allow some true due to randomness)
    ok(
        $false_count > $total_tests * 0.99,
        "Very low probability produces mostly false: $false_count/$total_tests"
    );

    my $true_count    = $total_tests - $false_count;
    my $observed_prob = $true_count / $total_tests;
    my $expected_prob = 0.00001;                       # 1/100000

    note("Observed probability: $observed_prob");
    note("Expected probability: $expected_prob");
    note("True results: $true_count, False results: $false_count");

    # With such low probability, we might get 0 trues, which is acceptable
    ok(
        $true_count <= $total_tests * 0.05,
        "True count within expected range for very low probability"
    );
};

subtest 'Extreme high probability validation' => sub {

    # Test very high probability (should be mostly true)
    my $high_config = {
        rule        => { true => 1 },
        sample_size => 99999,
        fleet_size  => 100000           # 99.999% probability
    };

    my $true_count  = 0;
    my $total_tests = 1000;

    for my $i ( 1 .. $total_tests ) {
        my $filter = Cpanel::Plugins::Components::Rules::Random->new($high_config);
        $true_count++ if $filter->is_allowed();
    }

    ok(
        $true_count > $total_tests * 0.95,
        "Very high probability produces mostly true: $true_count/$total_tests"
    );

    my $false_count   = $total_tests - $true_count;
    my $observed_prob = $true_count / $total_tests;
    my $expected_prob = 0.99999;

    note("Observed probability: $observed_prob");
    note("Expected probability: $expected_prob");
    note("True results: $true_count, False results: $false_count");

    # With such high probability, we might get all trues, which is acceptable
    ok(
        $false_count <= $total_tests * 0.05,
        "False count within expected range for very high probability"
    );
};

subtest 'Fleet size equals sample size (100% probability)' => sub {
    my $config = {
        rule        => { true => 1 },
        sample_size => 1000,
        fleet_size  => 1000             # 100% probability
    };

    my $true_count  = 0;
    my $total_tests = 100;

    for my $i ( 1 .. $total_tests ) {
        my $filter = Cpanel::Plugins::Components::Rules::Random->new($config);
        $true_count++ if $filter->is_allowed();
    }

    # Should be 100% true when sample_size == fleet_size
    is(
        $true_count, $total_tests,
        "100% probability when sample_size equals fleet_size"
    );

    note("All $total_tests tests returned true as expected");
};

subtest 'Minimum valid sample size' => sub {
    my $config = {
        rule        => { true => 1 },
        sample_size => 1,
        fleet_size  => 2                # 50% probability
    };

    my $true_count  = 0;
    my $total_tests = 1000;

    for my $i ( 1 .. $total_tests ) {
        my $filter = Cpanel::Plugins::Components::Rules::Random->new($config);
        $true_count++ if $filter->is_allowed();
    }

    my $observed_prob = $true_count / $total_tests;
    my $expected_prob = 0.5;

    # Allow reasonable variance for 50% probability
    ok(
        abs( $observed_prob - $expected_prob ) < 0.1,
        sprintf(
            "Minimum sample size works: observed %.3f vs expected %.3f",
            $observed_prob, $expected_prob
        )
    );

    note("True count: $true_count/$total_tests (${observed_prob})");
};

subtest 'Large fleet size stress test' => sub {
    my $config = {
        rule        => { true => 1 },
        sample_size => 1000,
        fleet_size  => 10000000         # Very large fleet, 0.01% probability
    };

    my $true_count  = 0;
    my $total_tests = 10000;            # More tests for better statistics

    for my $i ( 1 .. $total_tests ) {
        my $filter = Cpanel::Plugins::Components::Rules::Random->new($config);
        $true_count++ if $filter->is_allowed();
    }

    my $observed_prob = $true_count / $total_tests;
    my $expected_prob = 0.0001;                       # 1000/10000000

    # With very low probability, allow wide variance
    ok(
        $observed_prob <= 0.01,                       # Should be much less than 1%
        sprintf( "Large fleet size produces low selection rate: %.4f", $observed_prob )
    );

    note("True count: $true_count/$total_tests (${observed_prob})");
    note("Expected probability: $expected_prob");
};

subtest 'Probability precision at various scales' => sub {
    my @test_cases = (
        { sample_size => 1,    fleet_size => 10,    expected => 0.1, name => '10% (small scale)' },
        { sample_size => 10,   fleet_size => 100,   expected => 0.1, name => '10% (medium scale)' },
        { sample_size => 100,  fleet_size => 1000,  expected => 0.1, name => '10% (large scale)' },
        { sample_size => 1000, fleet_size => 10000, expected => 0.1, name => '10% (very large scale)' },
    );

    for my $case (@test_cases) {
        subtest $case->{name} => sub {
            my $config = {
                rule        => { true => 1 },
                sample_size => $case->{sample_size},
                fleet_size  => $case->{fleet_size}
            };

            my $total_tests = 500;
            my $true_count  = 0;

            for my $i ( 1 .. $total_tests ) {
                my $filter = Cpanel::Plugins::Components::Rules::Random->new($config);
                $true_count++ if $filter->is_allowed();
            }

            my $observed = $true_count / $total_tests;
            my $std_dev  = sqrt( $case->{expected} * ( 1 - $case->{expected} ) / $total_tests );
            my $margin   = 3 * $std_dev;                                                           # 99.7% confidence

            ok(
                abs( $observed - $case->{expected} ) < $margin,
                sprintf(
                    "Scale %d/%d: observed %.3f within %.3f of expected %.3f",
                    $case->{sample_size}, $case->{fleet_size},
                    $observed, $margin, $case->{expected}
                )
            );

            note("Observed: $true_count/$total_tests (${observed})");
        };
    }
};

done_testing;
