#!/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

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 File::Temp       qw/tempdir/;

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

# Mock company ID for testing
my $mock_company_id = 100;
my $mock_cid        = Test::MockModule->new("Cpanel::License::CompanyID");
$mock_cid->redefine(
    get_company_id => sub {
        return $mock_company_id;
    }
);

# Mock feature flags for testing
my $mock_feature_enabled = 1;
my $mock_feature         = Test::MockModule->new('Cpanel::FeatureFlags');
$mock_feature->redefine(
    is_feature_enabled => sub {
        return $mock_feature_enabled;
    }
);

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

subtest 'Integration with AND rules' => sub {
    my $temp_dir = tempdir( CLEANUP => 1 );

    subtest 'Random with AND rule' => sub {
        my $config = {
            rule => [
                'company-id'   => [ 100, 200 ],
                'feature-flag' => 'test.feature'
            ],
            sample_size => 1000,
            cache_file  => "$temp_dir/and_test_cache.dat"
        };

        # Both company-id and feature-flag should pass
        $mock_company_id      = 100;
        $mock_feature_enabled = 1;

        my $filter = Cpanel::Plugins::Components::Rules::Random->new($config);
        my $result = $filter->is_allowed();

        ok( $result == 0 || $result == 1, 'AND rule with Random returns boolean' );

        # Verify caching works
        my $second_result = $filter->is_allowed();
        is( $second_result, $result, 'Cached result is consistent for AND rule' );
    };

    subtest 'Random with failing AND rule' => sub {
        my $config = {
            rule => [
                'company-id'   => [ 100, 200 ],
                'feature-flag' => 'test.feature'
            ],
            sample_size => 1000
        };

        # Company-id passes but feature-flag fails
        $mock_company_id      = 100;
        $mock_feature_enabled = 0;

        my $filter = Cpanel::Plugins::Components::Rules::Random->new($config);
        my $result = $filter->is_allowed();

        ok( !$result, 'Random returns false when AND rule fails' );
    };
};

subtest 'Random distribution behavior' => sub {

    subtest 'distribution test with multiple instances' => sub {

        # Test that different Random instances with same config can produce different results
        my $config = {
            rule        => { 'company-id' => [100] },
            sample_size => 1000                         # Medium probability
        };

        $mock_company_id = 100;                         # Ensure rule passes

        my @results;
        for my $i ( 1 .. 20 ) {
            my $filter = Cpanel::Plugins::Components::Rules::Random->new($config);
            push @results, $filter->is_allowed();
        }

        # We should get a mix of true and false results (though randomness means this could vary)
        my $true_count  = grep { $_ } @results;
        my $false_count = @results - $true_count;

        ok(
            $true_count >= 0 && $false_count >= 0,
            "Random distribution produces mix of results: $true_count true, $false_count false"
        );

        # All results should be boolean
        for my $i ( 0 .. $#results ) {
            ok( $results[$i] == 0 || $results[$i] == 1, "Result $i is boolean" );
        }
    };

    subtest 'very high sample size produces mostly true results' => sub {
        my $config = {
            rule        => { 'company-id' => [100] },
            sample_size => 90000                        # Very high probability
        };

        $mock_company_id = 100;

        my $true_count  = 0;
        my $total_tests = 50;

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

        # With very high sample_size, we should get mostly true results
        ok(
            $true_count > $total_tests * 0.5,
            "High sample_size produces reasonable number of true results: $true_count/$total_tests"
        );
    };
};

subtest 'Cache persistence across different rule evaluations' => sub {
    my $temp_dir   = tempdir( CLEANUP => 1 );
    my $cache_file = "$temp_dir/persistence_test_cache.dat";

    subtest 'cache persists across different filter instances' => sub {
        my $config = {
            rule        => { 'company-id' => [100] },
            sample_size => 1000,
            cache_file  => $cache_file
        };

        $mock_company_id = 100;

        # First instance
        my $filter1 = Cpanel::Plugins::Components::Rules::Random->new($config);
        my $result1 = $filter1->is_allowed();

        # Second instance with same config should get same result
        my $filter2 = Cpanel::Plugins::Components::Rules::Random->new($config);
        my $result2 = $filter2->is_allowed();

        is( $result2, $result1, 'Cache persists across different filter instances' );

        # Third instance should also get same result
        my $filter3 = Cpanel::Plugins::Components::Rules::Random->new($config);
        my $result3 = $filter3->is_allowed();

        is( $result3, $result1, 'Cache remains consistent across multiple instances' );
    };
};

done_testing;

# END Augment generated code block
