#                                       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::Hooks::ServerRoleChange;

# Return errors if Perl experiences problems.
use cPstrict;

# Properly decode JSON.
use JSON;
use Cpanel::Imports;

my %systemd_360_service_mapping = (
    'httpd'    => 'httpd',
    'mysql'    => 'mysql',
    'exim'     => 'exim',
    'nginx'    => 'nginx',
    'powerdns' => 'powerdns',
);

# Embed hook attributes alongside the action code.
sub describe {
    my $enable_hook = {
        'category' => 'Whostmgr',
        'event'    => 'Server::Type::Change::enable',
        'stage'    => 'post',
        'hook'     => 'Whostmgr::360Monitoring::Hooks::ServerRoleChange::add',
        'exectype' => 'module',
    };

    my $disable_hook = {
        'category' => 'Whostmgr',
        'event'    => 'Server::Type::Change::disable',
        'stage'    => 'post',
        'hook'     => 'Whostmgr::360Monitoring::Hooks::ServerRoleChange::add',
        'exectype' => 'module',
    };

    return [ $enable_hook, $disable_hook ];
}

sub add ( $context, $data ) {

    # Get the data that the system passes to the hook.

    my ( $result, $message ) = ( 0, '' );

    my $ok;

    if ( $context->{event} eq 'Server::Type::Change::enable' ) {
        $ok = setup_360_monitoring($data);
    }
    elsif ( $context->{event} eq 'Server::Type::Change::disable' ) {
        $ok = disable_360_monitoring($data);
    }

    if ($ok) {
        $result  = 1;                               # This Boolean value is set to succeed.
        $message = 'This is a success message.';    # This string is a reason for $result.
    }
    else {
        $result  = 0;                               # This Boolean value is set to fail.
        $message = 'This is a failure message.';    # This string is a reason for $result.
    }

    return ( $result, $message );
}

sub setup_360_monitoring ($data) {
    my $services_ar      = $data->{'services'};
    my $changed_roles_ar = $data->{'changed_roles'};
    if (   !$services_ar
        || ref $services_ar ne 'ARRAY'
        || @{$services_ar} == 0 ) {
        return;
    }
    require Whostmgr::360Monitoring::Agent;
    my $agent         = Whostmgr::360Monitoring::Agent::load_from_file();
    my $needs_install = 0;
    foreach my $service (@$services_ar) {
        my $service_360 = $systemd_360_service_mapping{$service} // '';
        next if !$service_360;

        # for now just referring the default_plugins
        # will make it more streamlined once we finalize
        if ( grep { $_ eq $service_360 } @{ $agent->default_plugins } ) {
            $needs_install = 1;
            last;
        }
    }
    my $status = $needs_install ? $agent->install_plugins() : 0;
    return $status;
}

sub disable_360_monitoring ($data) {
    my $services_ar      = $data->{'services'};
    my $changed_roles_ar = $data->{'changed_roles'};
    use Whostmgr::360Monitoring::Agent;
    my $agent           = Whostmgr::360Monitoring::Agent::load_from_file();
    my $needs_uninstall = 0;
    foreach my $service (@$services_ar) {
        my $service_360 = $systemd_360_service_mapping{$service} // '';
        next if !$service_360;

        # for now just referring the default_plugins
        # will make it more streamlined once we finalize
        next if !grep { $_ eq $service_360 } @{ $agent->default_plugins };
        if ( my $service_plugin = $agent->get_plugin($service_360) ) {
            $needs_uninstall = 1;
            last;
        }
    }
    my $status = $needs_uninstall ? $agent->uninstall_plugins() : 0;
    return $status;
}

1;
