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

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::MockFile   qw/strict/;

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

use Cpanel::Plugins::Components::Rules::Personalization;

my $mock_value = {};
my $mock       = Test::MockModule->new('Whostmgr::API::1::Personalization');
$mock->redefine(
    personalization_get => sub ( $args, $metadata ) {
        return $mock_value;
    }
);

sub mock_return ($value) {
    $mock_value->{personalization} = {
        enabled => {
            value => $value,
        }
    };
}

subtest 'Check personalization dies when unsupported app passed' => sub {
    my $given = {
        app  => "cpanel",
        name => "enabled",
    };

    mock_return(undef);
    like(
        dies { Cpanel::Plugins::Components::Rules::Personalization->new($given)->is_allowed() },
        qr/Cpanel::Exception::InvalidParameter/,
        "Got exception for cpanel app"
    );
};

subtest 'Check personalization dies when unsupported operator passed' => sub {
    my $given = {
        app      => "whostmgr",
        name     => "enabled",
        operator => "uhoh",
    };

    mock_return(undef);
    like(
        dies { Cpanel::Plugins::Components::Rules::Personalization->new($given)->is_allowed() },
        qr/Cpanel::Exception::InvalidParameter/,
        "Got exception for unrecogized operator"
    );
};

subtest 'Check personalization when boolean with minimal required properties' => sub {
    my $given = {
        name => "enabled",
    };

    mock_return(undef);
    my $got = Cpanel::Plugins::Components::Rules::Personalization->new($given)->is_allowed();
    is $got, !!0, "got false value when enabled not set in personalization data.";

    mock_return(0);
    $got = Cpanel::Plugins::Components::Rules::Personalization->new($given)->is_allowed();
    is $got, !!0, "got false value when enabled is false in personalization data.";

    mock_return(1);
    $got = Cpanel::Plugins::Components::Rules::Personalization->new($given)->is_allowed();
    is $got, !!1, "got true value when enabled is true in personalization data.";
};

subtest 'Check personalization when all config properties are predefined' => sub {
    my $given = {
        app      => "whostmgr",
        store    => "group",
        name     => "enabled",
        operator => "boolean",
        default  => !!1,
    };

    mock_return(undef);
    my $got = Cpanel::Plugins::Components::Rules::Personalization->new($given)->is_allowed();
    is $got, !!1, "got true (default) value when enabled not set in personalization data.";

    mock_return(0);
    $got = Cpanel::Plugins::Components::Rules::Personalization->new($given)->is_allowed();
    is $got, !!0, "got false value when enabled is false in personalization data.";

    mock_return(1);
    $got = Cpanel::Plugins::Components::Rules::Personalization->new($given)->is_allowed();
    is $got, !!1, "got true value when enabled is true in personalization data.";
};

done_testing;
