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

use Moo;
use cPstrict;

use Whostmgr::360Monitoring::ApiClient ();
use Cpanel::License::CompanyID         ();
use Cpanel::Imports;
use Cpanel::Slurper                ();
use Whostmgr::360Monitoring::Agent ();

use constant UUID_FILE => '/var/cpanel/cpanel.uuid';

=head1 MODULE

C<Whostmgr::360Monitoring::Base>

=head1 DESCRIPTION

C<Whostmgr::360Monitoring::Base> is the base class that provides attributes inherited by various different 360Monitoring subclasses.

=head1 ATTRIBUTES

=head2 username - string

The username of the cPanel user with which the target 360Monitoring account is/will be associated. The user must exist.

=cut

has 'username' => (
    is  => 'rw',
    isa => sub {
        require Cpanel::Validate::Username;
        Cpanel::Validate::Username::user_exists_or_die( $_[0] );
    },
    default => sub ($self) { return $Cpanel::user || die 'Must set the username attribute.' }
);

=head2 company_id - string

Fetch the company id for the server.

=cut

has 'company_id' => (
    is  => 'ro',
    isa => sub {
        die locale->maketext("[asis,company_id] is not defined!") unless defined $_[0];
    },
    default => sub ($self) { return Cpanel::License::CompanyID::get_company_id() }
);

=head2 server_id - string

Fetch the unique server id for the server.

=cut

has 'server_id' => (
    is  => 'ro',
    isa => sub {
        die locale->maketext("[asis,server_id] is not defined!") unless defined $_[0];
    },
    default => sub ($self) {
        chomp( my $uuid = eval { Cpanel::Slurper::read(UUID_FILE) } || '' );
        return $uuid;
    }
);

=head2 api - C<Cpanel::Plugins::RestApiClient> instance

The API client that will be used to make calls against the 360Monitoring API backend.

=cut

has 'api' => (
    is      => 'ro',
    lazy    => 1,
    isa     => \&Whostmgr::360Monitoring::Validate::valid_api_object,
    builder => 1
);

=head2 auth_url - string

The base URL to use for authentication requests.
The URL varies based on environment.

=cut

has 'auth_url' => (
    is      => 'ro',
    lazy    => 1,
    isa     => \&Whostmgr::360Monitoring::Validate::valid_api_url,
    default => sub ($self) {
        return Whostmgr::360Monitoring::Agent::IS_STAGING ? 'https://auth.stage.koalityengine.com/v1/cpanel/' : 'https://auth.koalityengine.com/v1/cpanel/';
    }
);

=head2 app360_url - string

The base URL to use for App360 requests.
App360 is used as a middleman in the creation of a new user and provides the new user's app token, itself used for session authentication.
The URL varies based on environment.

=cut

has 'app360_url' => (
    is      => 'ro',
    lazy    => 1,
    isa     => \&Whostmgr::360Monitoring::Validate::valid_api_url,
    default => sub ($self) {
        return Whostmgr::360Monitoring::Agent::IS_STAGING ? 'https://cpanel.stage.360monitoring.com/' : 'https://cpanel.360monitoring.com/';
    }
);

sub _build_api ($self) {
    return Whostmgr::360Monitoring::ApiClient->new( username => $self->username );
}

1;
