package Whostmgr::API::1::EcosystemFeatures;

=encoding utf8

=head1 NAME

Whostmgr::API::1::EcosystemFeatures

=head1 DESCRIPTION

This module contains whmapi methods related to Plugins..

=head1 FUNCTIONS

=cut

use cPstrict;
use Cpanel::Exception       ();
use Whostmgr::API::1::Utils ();
use Common::Plugins::Setup  ();

use constant NEEDS_ROLE => {
    local_enable  => undef,
    local_disable => undef,
    local_reset   => undef,
};

my $logger;

=head2 local_enable()

This function allows a local administrator to enable a plugin. The
checks it bypasses are dependent on the plugin's local_enable call.

=head3 ARGUMENTS

=over

=item plugin - string Required. The plugin name in all lowercase,
separated by dashes. For example:

cpanel-monitoring-plugin

=item namespace - string Optional. The namespace of the plugin. Defaults to 'Whostmgr'.
The value must be one of the following: Whostmgr, Cpanel.

=back

=cut

sub local_enable {
    my ( $args, $metadata ) = @_;

    my $plugin_name = Whostmgr::API::1::Utils::get_required_argument( $args, 'plugin' );
    my $namespace   = Whostmgr::API::1::Utils::get_length_argument( $args, 'namespace' ) || 'Whostmgr';

    Common::Plugins::Setup::run_from_module( $namespace, $plugin_name, 'enable' );

    Whostmgr::API::1::Utils::set_metadata_ok($metadata);

    return;
}

=head2 local_disable()

This function allows a local administrator to disable a plugin for
the server, instead of it being forced-on from Webpros.

After local_disable() has been used, local_enable() must be used to
re-enable it.

=head3 ARGUMENTS

=over

=item plugin - string Required. The plugin name in all lowercase,
separated by dashes. For example:

cpanel-monitoring-plugin

=item namespace - string Optional. The namespace of the plugin. Defaults to 'Whostmgr'.
The value must be one of the following: Whostmgr, Cpanel.

=back

=cut

sub local_disable {
    my ( $args, $metadata ) = @_;

    my $plugin_name = Whostmgr::API::1::Utils::get_required_argument( $args, 'plugin' );
    my $namespace   = Whostmgr::API::1::Utils::get_length_argument( $args, 'namespace' ) || 'Whostmgr';

    Common::Plugins::Setup::run_from_module( $namespace, $plugin_name, 'disable' );

    Whostmgr::API::1::Utils::set_metadata_ok($metadata);

    return;
}

=head2 local_reset()

This function allows a local administrator to reset a plugin back to its
default configuration state. This will remove any local enable/disable
overrides and restore the plugin to its original ecosystem-managed state.

This is useful when you want to:

    - Remove local customizations and return to ecosystem defaults
    - Clear any configuration issues that may have occurred
    - Reset plugin state after troubleshooting

=head3 ARGUMENTS

=over

=item plugin - string Required. The plugin name in all lowercase,
separated by dashes. For example:

cpanel-sitejet-plugin

=item namespace - string Optional. The namespace of the plugin. Defaults to 'Whostmgr'.
The value must be one of the following: Whostmgr, Cpanel.

=back

=cut

sub local_reset {
    my ( $args, $metadata ) = @_;

    my $plugin_name = Whostmgr::API::1::Utils::get_required_argument( $args, 'plugin' );
    my $namespace   = Whostmgr::API::1::Utils::get_length_argument( $args, 'namespace' ) || 'Whostmgr';

    Common::Plugins::Setup::run_from_module( $namespace, $plugin_name, 'reset' );

    Whostmgr::API::1::Utils::set_metadata_ok($metadata);

    return;
}

sub APPLIST {
    return {
        local_enable => [
            namespace => 'EcosystemFeatures',
            method    => 'local_enable',
            argsmode  => 'strict',
        ],
        local_disable => [
            namespace => 'EcosystemFeatures',
            method    => 'local_disable',
            argsmode  => 'strict',
        ],
        local_reset => [
            namespace => 'EcosystemFeatures',
            method    => 'local_reset',
            argsmode  => 'strict',
        ],
    };
}

1;
