#!/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 Test::Cpanel::Policy testing => 'Cpanel::Plugins::Components::Rules::Filter';

use Cpanel::Plugins::Components::Rules::True   ();
use Cpanel::Plugins::Components::Rules::False  ();
use Cpanel::Plugins::Components::Rules::Filter ();

# Create a test filter implementation for testing the base class
{

    package TestFilter;
    use Moo;
    extends 'Cpanel::Plugins::Components::Rules::Filter';

    sub apply_filter ( $self, $rule_result, $config ) {
        return $rule_result;
    }
}

subtest 'Filter base class construction' => sub {

    subtest 'with hash configuration' => sub {
        my $config = {
            rule       => { true => 1 },
            test_param => 'value'
        };

        my $filter = TestFilter->new($config);
        is $filter->config, $config, 'Filter constructed with hash config';
    };
};

subtest 'Configuration validation' => sub {

    subtest 'missing rule defaults to true' => sub {
        my $filter = TestFilter->new();
        ok(

            lives { $filter->is_allowed() },
            'Lives when rule is not passed since defaults to true() as base rule'
        ) or diag explain($@);

        is $filter->is_allowed(), !!1, 'Missing rule defaults to true';
    };

    subtest 'invalid config type fails' => sub {

        # Create filter with string config directly (bypassing BUILDARGS)
        my $filter = bless { config => 'invalid' }, 'TestFilter';
        like dies { $filter->is_allowed() },
          qr/Filter configuration must be a hash and must include a rule./,
          'Dies with invalid config type';
    };
};

subtest 'Rule evaluation' => sub {

    subtest 'hash format rule' => sub {
        my $config = { rule => { true => 1 } };

        my $filter = TestFilter->new($config);
        is $filter->is_allowed(), !!1, 'Hash format rule evaluates correctly';
    };

    subtest 'array format rule' => sub {
        my $config = { rule => [ true => 1 ] };

        my $filter = TestFilter->new($config);
        is $filter->is_allowed(), !!1, 'Array format rule evaluates correctly';
    };
};

subtest 'Filter application' => sub {

    subtest 'identify filter returns true when rule returns true' => sub {
        my $config = {
            rule       => { true => 1 },
            test_param => 'value'
        };

        my $filter = TestFilter->new($config);
        is $filter->is_allowed(), !!1, 'Filter passes when both rule and filter pass';
    };

    subtest 'identity filter returns false when rule returns false' => sub {
        my $config = {
            rule       => { false => 1 },
            test_param => 'value'
        };

        my $filter = TestFilter->new($config);
        is $filter->is_allowed(), !!0, 'Filter fails when rule fails';
    };
};

subtest 'Base class apply_filter method' => sub {

    subtest 'base class apply_filter dies' => sub {
        my $config = { rule => { true => 1 } };

        my $filter = Cpanel::Plugins::Components::Rules::Filter->new($config);
        like dies { $filter->is_allowed() },
          qr/apply_filter method must be implemented by subclass/,
          'Base class apply_filter method dies with appropriate message';
    };
};

done_testing;

# END Augment generated code block
