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

use cPstrict;
use Cpanel::Imports;

=head1 MODULE

C<Whostmgr::360Monitoring::Validate>

=head1 DESCRIPTION

C<Whostmgr::360Monitoring::Validate> provides validation methods for 360Monitoring API related inputs.
This module is primarily geared for use in type checking the C<Whostmgr::360Monitoring::Base> class' attributes as well as those of related subclasses.

=head1 FUNCTIONS

=head2 valid_api_url( url )

Validate an API URL.

=head3 ARGUMENTS

=over

=item url - string - Required

The API url to validate.

=back

=head3 RETURNS

Returns 1 if the URL is valid and dies if the URL is invalid.

=head3 EXAMPLES

has 'auth_url' => (

...

    isa     => \&Whostmgr::360Monitoring::Validate::valid_api_url,

...

);

=cut

sub valid_api_url ($url) {

    # It's ok if url is undefined we may not have one yet.
    return 1 if !$url;

    require Cpanel::Validate::URL;
    die locale()->maketext("Invalid URL.")                if !Cpanel::Validate::URL::is_valid_url($url);
    die locale()->maketext("Must start with “https://”.") if $url !~ /^https:\/\//m;
    die locale()->maketext("Must end with a “/”.")        if $url !~ /\/$/m;

    return 1;
}

=head2 valid_api_object( object )

Validate that the input is a C<Cpanel::Plugins::RestApiClient> object.

=head3 ARGUMENTS

=over

=item object - blessed reference - Required

The API object to validate.

=back

=head3 RETURNS

Returns 1 if the object is valid and dies if the object is invalid.

=head3 EXAMPLES

has 'api' => (

...

    isa     => \&Whostmgr::360Monitoring::Validate::valid_api_object,

...

);

=cut

sub valid_api_object ($obj) {
    require Scalar::Util;
    die 'Not a blessed perl object.'                       if !Scalar::Util::blessed($obj);
    die 'Must be a Cpanel::Plugins::RestApiClient object.' if !$obj->isa('Cpanel::Plugins::RestApiClient');

    return 1;
}

=head2 valid_token( token )

Validate that the input is a valid token that doesn't include any illegal characters.

=head3 ARGUMENTS

=over

=item token - string - Required

The token to validate.

=back

=head3 RETURNS

Returns 1 if the token is valid and dies if the token is invalid.

=head3 EXAMPLES

has 'app_token' => (

...

    isa     => \&Whostmgr::360Monitoring::Validate::valid_token,

...

);

=cut

sub valid_token ($token) {

    # It's ok if token is undefined we may not have one yet.
    return 1 if !$token;

    die locale()->maketext('Invalid token.') if $token !~ /^[a-zA-Z0-9-_.]+$/m;
    return 1;
}

=head2 valid_locale( locale )

Validate that the input is a valid locale that is supported by 360Monitoring.

=head3 ARGUMENTS

=over

=item locale - string - Required

The locale string to validate.

=back

=head3 RETURNS

Dies if the locale is not a two language character code.

=head3 EXAMPLES

    isa => \&Whostmgr::360Monitoring::Validate::valid_locale,

=cut

sub valid_locale ($locale) {

    if ( !$locale || $locale !~ m/^[a-zA-Z]{2}$/ ) {
        die locale()->maketext("The users locale must be a ISO 639-1 two character language code.");
    }

    return 1;
}

=head2 valid_geo_location( location )

Validate the koality datacenter location.

The only possible locations are:
us_east
de
asia_jp

=head3 ARGUMENTS

=over

=item location - string - Required

The location to validate.

=back

=head3 RETURNS

Dies if the location is invalid, returns 1 otherwise.

=head3 EXAMPLES

    isa => \&Whostmgr::360Monitoring::Validate::valid_geo_location,

=cut

sub valid_geo_location ($location) {

    my @locations = ( 'us_east', 'de', 'asia_jp' );
    if ( !grep { $_ eq $location } @locations ) {
        die locale()->maketext( "Must be one of the following locations: [list_and_quoted,_1].", \@locations );
    }

    return 1;
}

1;
