#!/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
package Cpanel::Plugins::Components::Rules::True;
use cPstrict;
use Moo;
extends 'Cpanel::Plugins::Components::Rules::Base';

sub is_allowed ($self) {
    return !!1;
}

package Cpanel::Plugins::Components::Rules::False;
use cPstrict;
use Moo;
extends 'Cpanel::Plugins::Components::Rules::Base';

sub is_allowed ($self) {
    return !!0;
}

package main;

use cPstrict;

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

use Test::Cpanel::Policy testing => 'Cpanel::Plugins::Components::Rules::Not';
use Capture::Tiny;

$INC{'/usr/local/cpanel/Cpanel/Plugins/Components/Rules/True.pm'}  = 'inline';
$INC{'/usr/local/cpanel/Cpanel/Plugins/Components/Rules/False.pm'} = 'inline';

use Cpanel::Plugins::Components::Rules::Not;

subtest 'Test not() operator' => sub {

    subtest 'when starting with True operator' => sub {
        my $phrase = {
            true => 1,
        };
        my $got = Cpanel::Plugins::Components::Rules::Not->new($phrase)->is_allowed();
        is $got, !!0, "not true is false";
    };

    subtest 'when starting with False operator' => sub {
        my $phrase = {
            false => 1,
        };
        my $got = Cpanel::Plugins::Components::Rules::Not->new($phrase)->is_allowed();
        is $got, !!1, "not false is true";
    };

    subtest 'when starting with Or phrase array' => sub {
        my $phrase = [
            true => 1,
            true => 1,
        ];
        my $got = Cpanel::Plugins::Components::Rules::Not->new($phrase)->is_allowed();
        is $got, !!0, "not (true or true) is false";
    };

    subtest 'when starting with Or phrase hash' => sub {
        my $phrase = {
            or => [
                true  => 1,
                false => 1,
            ]
        };

        my $got = Cpanel::Plugins::Components::Rules::Not->new($phrase)->is_allowed();
        is $got, !!0, "not (true or false) is false";
    };

    subtest 'when starting with nested phrase' => sub {
        my $phrase = {
            and => [
                true => 1,
                true => 1,
            ]
        };
        my $got = Cpanel::Plugins::Components::Rules::Not->new($phrase)->is_allowed();
        is $got, !!0, "not (true and true) is false";
    };

    subtest 'when campaign_id is passed' => sub {
        my $mock_nvdata = Test::MockModule->new("Cpanel::Plugins::Components::Rules::Util::NVData");
        $mock_nvdata->redefine(
            get_nv_data => sub { 1 },
            set_nv_data => sub { 1 },
        );

        my $phrase = {
            and => [
                true => 1,
                true => 1,
            ]
        };

        my $not_object = Cpanel::Plugins::Components::Rules::Not->new($phrase);
        $not_object->{campaign_id} = '12345';
        my $got = $not_object->is_allowed();
        is $got, !!0, "NOT rule is processed with campaign_id";
    };
};

done_testing;
