#!/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::State';
use Capture::Tiny;

use Cpanel::Plugins::State ();

my $mock_state = {};
my $mock       = Test::MockModule->new('Cpanel::FeatureFlags');
$mock->redefine(
    enable => sub ($key) {
        $mock_state->{$key} = 1;
    },
    disable => sub ($key) {
        delete $mock_state->{$key};
    },
    is_feature_enabled => sub ($key) {
        defined $mock_state->{$key} ? 1 : 0;
    },
);

sub reset_mock() {
    my @keys = keys $mock_state->%*;
    foreach my $key (@keys) {
        delete $mock_state->{$key};
    }
}

my $plugin_id = "cpanel.helloworld.plugin";

subtest 'install' => sub {
    reset_mock();
    is Cpanel::Plugins::State::is_installed($plugin_id), '', "is_installed() returns false if plugin is not installed.";
    Cpanel::Plugins::State::install($plugin_id);
    is Cpanel::Plugins::State::is_installed($plugin_id), 1, "is_installed() returns true if plugin is installed.";
};

subtest 'uninstall' => sub {
    reset_mock();
    Cpanel::Plugins::State::install($plugin_id);
    is Cpanel::Plugins::State::is_installed($plugin_id), 1, "is_installed() returns true if plugin is installed.";
    Cpanel::Plugins::State::uninstall($plugin_id);
    is Cpanel::Plugins::State::is_installed($plugin_id), "", "is_installed() returns false if plugin is not installed.";
};

subtest 'enable' => sub {
    reset_mock();
    Cpanel::Plugins::State::install($plugin_id);
    Cpanel::Plugins::State::enable($plugin_id);
    is Cpanel::Plugins::State::is_installed($plugin_id),          1, "is_installed() returns true if plugin is installed.";
    is Cpanel::Plugins::State::is_explicitly_enabled($plugin_id), 1, "is_explicitly_enabled() returns true if plugin is enabled locally.";
    is Cpanel::Plugins::State::is_enabled($plugin_id),            1, "is_enabled() returns true if plugin is enabled locally.";
};

subtest 'disable' => sub {
    reset_mock();
    Cpanel::Plugins::State::install($plugin_id);
    Cpanel::Plugins::State::disable($plugin_id);
    is Cpanel::Plugins::State::is_installed($plugin_id),           1,  "is_installed() returns true if plugin is installed.";
    is Cpanel::Plugins::State::is_explicitly_disabled($plugin_id), 1,  "is_explicitly_disabled() returns true if plugin is enabled locally.";
    is Cpanel::Plugins::State::is_enabled($plugin_id),             "", "is_enabled() returns false if plugin is enabled locally.";
};

subtest 'is_enabled' => sub {
    reset_mock();
    Cpanel::Plugins::State::install($plugin_id);
    is Cpanel::Plugins::State::is_installed($plugin_id),           1,   "is_installed() returns true if plugin is installed.";
    is Cpanel::Plugins::State::is_explicitly_disabled($plugin_id), '',  "is_explicitly_disabled() returns false if plugin is not disabled locally.";
    is Cpanel::Plugins::State::is_explicitly_enabled($plugin_id),  '',  "is_explicitly_enabled() returns false if plugin is not enabled locally.";
    is Cpanel::Plugins::State::is_enabled($plugin_id),             "?", "is_enabled() returns unknown.";
};

done_testing;
