
#                                      Copyright 2024 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 Whostmgr::360Monitoring::Agent::Plugins;

use cPstrict;
use Moo;
use Types::Standard                qw(InstanceOf);
use Types::Common::String          qw(NonEmptyStr);
use Whostmgr::360Monitoring::Agent ();
use experimental 'builtin';

with qw(
  Whostmgr::360Monitoring::Agent::Plugins::Roles::Enableable
  Whostmgr::360Monitoring::Agent::Plugins::Roles::Runnable
  Whostmgr::360Monitoring::Agent::Plugins::Roles::Serializable
);

has 'agent' => (
    is       => 'ro',
    isa      => InstanceOf ['Whostmgr::360Monitoring::Agent'],
    required => 1,
    init_arg => '_agent'
);

has 'name' => (
    is      => 'ro',
    isa     => NonEmptyStr,
    default => sub ($self) {
        my $class = builtin::blessed($self);
        $class =~ m/.*::([^:]+?)$/;
        return lc($1);
    }
);

sub _new ( $class, $options = {} ) {
    return $class->new( Whostmgr::360Monitoring::Agent->new(), $options );
}

around 'BUILDARGS' => sub ( $orig, $class, $agent, $options = {}, @args ) {
    my %options = %$options;
    $options{_agent} = $agent;
    die "Too many arguements" if @args;
    die "Creating a plugin requires an agent as the first argument to new" unless $agent;
    return $class->$orig( \%options );
};

1;

__END__

=head1 NAME

Whostmgr::360Monitoring::Agent::Plugins - Base class for all Plugins

=head1 SYNOPSIS

    # Can be any plugin, here we use CloudLinux as an example
    my $class = 'Whostmgr::360Monitoring::Agent::Plugins::Cloudlinux';

    # To create a plugin, you need an agent to contain it.
    my $agent  = 'Whostmgr::360Monitoring::Agent'->new();

    # Instatiate a plugin by providing an agent as the first argument
    my $plugin = $class->new( $agent, $options );

    $agent->settings_global_plugin->enabled(0);
    $plugin->enabled(1);
    $plugin->agent->settings_global_plugin->enabled(1);
