#                                      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.

package Whostmgr::CometBackup::AgentManager;

use Cpanel::Systemd ();

our $AGENT_COMET = 'backup-tool';

sub stop {
    eval { Cpanel::Systemd::systemctl( 'stop', "${AGENT_COMET}.service" ) };
    if ($@) {
        return ( 0, "The systemctl command failed because of an error: $@" );
    }

    # Verify the service stopped
    my $state = eval { _get_service_state($AGENT_COMET) };
    if ($@) {
        return ( 0, "The systemctl command failed because of an error: $@" );
    }
    if ( !defined $state || $state ne 'inactive' ) {
        return ( 0, "Failed to stop the service, $AGENT_COMET, automatically." );
    }
    return ( 1, "stopped" );
}

sub restart {
    eval { Cpanel::Systemd::systemctl( 'restart', "${AGENT_COMET}.service" ) };
    if ($@) {
        return ( 0, "The systemctl command failed because of an error: $@" );
    }

    # Verify the service is active
    my $state = eval { _get_service_state($AGENT_COMET) };
    if ($@) {
        return ( 0, "The systemctl command failed because of an error: $@" );
    }
    if ( !defined $state ) {
        return ( 0, "The $AGENT_COMET service is not a known service." );
    }
    if ( $state ne 'active' ) {
        return ( 0, "The $AGENT_COMET service failed to start." );
    }
    return ( 1, "restarted" );
}

sub start {
    my ( $status, $message ) = restart();
    return ( $status, $status ? "started" : "Failed to start: $message" );
}

sub get_status {
    my $status = eval { _get_service_state($AGENT_COMET) } // '';
    if ($@) {
        return ( 0, "The systemctl command failed because of an error: $@" );
    }
    return ( 1, $status );
}

sub disable {
    eval { Cpanel::Systemd::systemctl( 'disable', '--now', "${AGENT_COMET}.service" ) };
    if ($@) {
        return ( 0, "The systemctl command failed because of an error: $@" );
    }

    my $status = eval { _get_service_state($AGENT_COMET) } // '';
    if ($@) {
        return ( 0, "The systemctl command failed because of an error: $@" );
    }

    return ( 1, $status );
}

# Helper function to get service ActiveState
sub _get_service_state {
    my ($service) = @_;

    my $service_name = $service;
    $service_name .= '.service' if $service_name !~ /\.service$/;

    my $run    = Cpanel::Systemd::systemctl( 'show', '-p', 'ActiveState', $service_name );
    my $stdout = $run->stdout();

    if ( $stdout =~ /^ActiveState=(.+)$/m ) {
        return $1;
    }

    return undef;
}

1;

__END__

=head1 NAME

Whostmgr::CometBackup::AgentManager - A class for managing the Comet Backup agent service

=head1 DESCRIPTION

A class that starts, stops, and restarts the Comet Backup agent service.

=head1 SYNOPSIS

    use Whostmgr::CometBackup::AgentManager ();

    my ( $status, $message ) = Whostmgr::CometBackup::AgentManager::start();
    if ( !$status ) {
        warn "Failed to start: $message";
    }

    my ( $status, $message ) = Whostmgr::CometBackup::AgentManager::restart();
    my ( $status, $state ) = Whostmgr::CometBackup::AgentManager::get_status();

    my ( $status, $message ) = Whostmgr::CometBackup::AgentManager::stop();

=head1 METHODS

=head2 start

Starts the backuptool service via systemd.

=head3 RETURNS

Returns a list: ( $status, $message )

Where $status is 1 on success, 0 on failure.
And $message is "started" on success or an error description on failure.

=head2 stop

Stops the backuptool service via systemd.

=head3 RETURNS

Returns a list: ( $status, $message )

Where $status is 1 on success, 0 on failure.
And $message is "stopped" on success or an error description on failure.

=head2 restart

Restarts the backuptool service via systemd.

=head3 RETURNS

Returns a list: ( $status, $message )

Where $status is 1 on success, 0 on failure.
And $message is "restarted" on success or an error description on failure.

=head2 get_status

Retrieves the status of the backuptool service via systemd.

=head3 RETURNS

Returns a list: ( $status, $state )

Where $status is 1 on success, 0 on failure.
And $state is the service state (e.g., 'active', 'inactive') on success or an error description on failure.

=cut
