#                                      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::AgentManager;

use Cpanel::RestartSrv::Systemd ();
use Cpanel::Exception           ();

our $AGENT360 = 'agent360';

sub stop {
    my $state = eval { Cpanel::RestartSrv::Systemd::stop_via_systemd($AGENT360) };
    if ($@) {
        die Cpanel::Exception::create( 'ProcessFailed', "The “[_1]” command failed because of an error: [_2]", [ "systemctl", $@ ] );
    }
    if ( !defined $state || $state ne 'inactive' ) {
        die Cpanel::Exception::create( 'ProcessFailed', "Failed to stop the service, [_1], automatically.", [$AGENT360] );
    }
    return 1;
}

sub restart {
    my ( $state, $pid ) = eval { Cpanel::RestartSrv::Systemd::restart_via_systemd($AGENT360) };
    if ($@) {
        die Cpanel::Exception::create( 'ProcessFailed', "The “[_1]” command failed because of an error: [_2]", [ "systemctl", $@ ] );
    }
    if ( !defined $state && !defined $pid ) {
        die Cpanel::Exception::create( 'InvalidParameter', "The “[_1]” service is not a known service.", [$AGENT360] );
    }
    if ( $state ne 'active' ) {
        die Cpanel::Exception::create( 'ProcessFailed', "The “[_1]” service failed to start.", [$AGENT360] );
    }
    return 1;
}

sub start {
    my $result = restart();
    return $result;
}

sub get_status {
    my $status = eval { Cpanel::RestartSrv::Systemd::get_status_via_systemd($AGENT360) } // '';
    if ($@) {
        die Cpanel::Exception::create( 'ProcessFailed', "The “[_1]” command failed because of an error: [_2]", [ "systemctl", $@ ] );
    }
    return $status;
}

sub disable {

    # TODO CPANEL-46095 call a public method instead of externally calling an internal one
    my $ret = eval { Cpanel::RestartSrv::Systemd::_run_systemctl( 'disable', '--now', 'agent360' ) } // '';
    if ($@) {
        die Cpanel::Exception::create( 'ProcessFailed', "The “[_1]” command failed because of an error: [_2]", [ "systemctl", $@ ] );
    }

    my $status = eval { Cpanel::RestartSrv::Systemd::get_status_via_systemd($AGENT360) } // '';
    if ($@) {
        die Cpanel::Exception::create( 'ProcessFailed', "The “[_1]” command failed because of an error: [_2]", [ "systemctl", $@ ] );
    }

    return $status;
}

1;

__END__

=head1 NAME

Whostmgr::360Monitoring::AgentManager - A class for managing the agent360 service, and services being monitored by it.

=head1 DESCRIPTION

A class that starts, stops, and restarts the services monitored by 360Monitoring,

along with the agent360 service itself.

=head1 SYNOPSIS

    use Whostmgr::360Monitoring::AgentManager ();

    my $start = Whostmgr::360Monitoring::AgentManager::start();
    my $status_start = Whostmgr::360Monitoring::AgentManager::get_status();

    my $restart = Whostmgr::360Monitoring::AgentManager::restart();
    my $status_restart = Whostmgr::360Monitoring::AgentManager::get_status();

    my $stop = Whostmgr::360Monitoring::AgentManager::stop();
    my $status_stop = Whostmgr::360Monitoring::AgentManager::get_status();

=head1 METHODS

=head2 start

Starts the agent360 service via systemd.

=head3 RETURNS

1 if successful, 0 on failure.

=head2 stop

Stops the agent360 service via systemd.

=head3 RETURNS

1 if successful, 0 on failure.

=head2 restart

Restarts the agent360 service via systemd.

=head3 RETURNS

1 if successful, 0 on failure.

=head2 get_status

Retrieves the status of the agent360 service via systemd.

=head3 RETURNS

Returns 'active' if service is running, otherwise returns 'inactive'.

=cut
