#!/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::MockModule qw/strict/;
use Test::Cpanel::Policy testing => 'Cpanel::Plugins::Components::Rules::And';
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::And;

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

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

        $phrase = [
            false => 1,
            true  => 1,
        ];
        $got = Cpanel::Plugins::Components::Rules::And->new($phrase)->is_allowed();
        is $got, !!0, "(false and true) is false";

        $phrase = [
            true  => 1,
            false => 1,
        ];
        $got = Cpanel::Plugins::Components::Rules::And->new($phrase)->is_allowed();
        is $got, !!0, "(true and false) is false";

        $phrase = [
            false => 1,
            false => 1,
        ];
        $got = Cpanel::Plugins::Components::Rules::And->new($phrase)->is_allowed();
        is $got, !!0, "(false and false) is false";
    };

    subtest 'when starting with And phrase hash' => sub {
        note "Order of operations not specified in a hash, defaults to sort order of keys";

        my $phrase = {
            true  => 1,
            false => 1,
        };

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

        $phrase = {
            true => 1,
            true => 1,
        };

        $got = Cpanel::Plugins::Components::Rules::And->new($phrase)->is_allowed();
        is $got, !!1, "(true and true) is true";
    };

    subtest 'when campaign_id is passed' => sub {
        my $mock_filter = Test::MockModule->new("Cpanel::Plugins::Components::Rules::Util::Filter");
        $mock_filter->redefine(
            find_date_range => sub {
                my $date_range = [
                    "date-range",
                    [
                        "start_date", "08-29-2024",
                        "end_date",   "10-30-2024"
                    ],
                ];
                return $date_range;
            }
        );
        my $mock_startdate = Test::MockModule->new("Cpanel::Plugins::Components::Rules::Util::FindStartDate");
        $mock_startdate->redefine(
            process_start_date => sub {
                1;
            }
        );

        my $phrase = [
            true => 1,
            true => 1,
        ];

        my $and_object = Cpanel::Plugins::Components::Rules::And->new($phrase);
        $and_object->{campaign_id} = '12345';
        my $got = $and_object->is_allowed();
        is $got, !!1, "AND rule is processed with campaign_id";
    };
};

done_testing;
