package Whostmgr::360Monitoring::Agent::Plugins::Roles::ServerProfile;
use Moo::Role;
use cPstrict;

use Cpanel::Server::Type;
use Cpanel::Server::Type::Profile;
use Cpanel::Server::Type::Profile::Roles;
use Types::Common qw(Str Ref NonEmptyStr ArrayRef);

requires qw/REQUIRED_ROLES REQUIRED_SERVICES/;

# perl -MCpanel::Server::Type::Profile::Roles -E"die Server::Type::Profile::Roles::is_role_enabled("WebServer");
sub check_roles ($self) {
    return !!1 unless defined $self->REQUIRED_ROLES;    # some plugins may not include a role check

    foreach my $role ( $self->REQUIRED_ROLES->@* ) {
        return !!0 unless Cpanel::Server::Type::Profile::Roles::is_role_enabled($role);
    }
    return !!1;
}

sub check_services ($self) {
    return !!1 unless defined $self->REQUIRED_SERVICES;    # some plugins may not include a service check

    state $services = Cpanel::Services::List::get_service_list();
    foreach my $service ( $self->REQUIRED_SERVICES->@* ) {
        return !!0 unless exists $services->{$service};
    }
    return !!1;
}

around 'is_installable' => sub ( $orig, $self, @args ) {
    return $self->check_services && $self->check_roles && $self->$orig(@args);
};

!!1;
