
#                                      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::API::1::360Monitoring;

use cPstrict;

use Whostmgr::API::1::Utils ();

sub register_server_with_360_monitoring {
    my ( $args, $metadata ) = @_;
    Whostmgr::API::1::Utils::set_metadata_ok($metadata);

    require Whostmgr::360Monitoring::User;
    require Cpanel::PwCache;

    my $username = Cpanel::PwCache::getusername();
    my $user     = Whostmgr::360Monitoring::User->new( username => $username );
    my $api_key  = $user->api_key;
    my $user_id  = $user->user_id;

    return {
        user_id => $user_id,
        api_key => $api_key,
        status  => 1,
    } if $api_key;

    require Whostmgr::360Monitoring::Setup;

    # In the case we never setup the monitor
    $user = Whostmgr::360Monitoring::Setup::initialize();
    return {
        user_id => $user->user_id,
        api_key => $user->api_key,
        status  => 1,
    };
}

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

    Whostmgr::API::1::Utils::set_metadata_ok($metadata);
    return { status => 1 };
}

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

    Whostmgr::API::1::Utils::set_metadata_ok($metadata);
    return { status => 1 };
}

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

    require Whostmgr::360Monitoring::User;
    require Cpanel::PwCache;

    my $username     = Cpanel::PwCache::getusername();
    my $user         = Whostmgr::360Monitoring::User->new( username => $username );
    my $is_monitored = $user->api_key && $user->user_id ? 1 : 0;

    Whostmgr::API::1::Utils::set_metadata_ok($metadata);
    return {
        is_monitored => $is_monitored,
    };
}

use constant NEEDS_ROLE => {
    register_server_with_360            => undef,
    disable_360_monitoring              => undef,
    enable_360_monitoring               => undef,
    register_server_with_360_monitoring => undef,
};

sub APPLIST {
    return {
        register_server_with_360_monitoring => [
            namespace => '360Monitoring',
            method    => 'register_server_with_360_monitoring',
            argsmode  => 'strict',
        ],
        disable_360_monitoring => [
            namespace => '360Monitoring',
            method    => 'disable_360_monitoring',
            argsmode  => 'strict',
        ],
        enable_360_monitoring => [
            namespace => '360Monitoring',
            method    => 'enable_360_monitoring',
            argsmode  => 'strict',
        ],
        is_server_monitored_by_360 => [
            namespace => '360Monitoring',
            method    => 'is_server_monitored_by_360',
            argsmode  => 'strict',
        ]
    };
}

1;
