#                                     Copyright 2025 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 Cpanel::Plugins::Components::Rules::IsBeta;
use cPstrict;
use Cpanel::Imports;

use Cpanel::Plugins::Components::Rules;
use Moo;
extends 'Cpanel::Plugins::Components::Rules::Base';

=head1 MODULE

C<Cpanel::Plugins::Components::Rules::IsBeta>

=head1 DESCRIPTION

Evaluates whether the current server is eligible to access the plugin's beta features.

This class is used when a rule  includes an 'is-beta' entry.

=head1 SYNOPSIS

Check if the server is eligible to access a plugin's beta features.

  use Cpanel::Plugins::Components::Rules::IsBeta;
  my $return = Cpanel::Plugins::Components::Rules::IsBeta->new('sitejet')->is_allowed();

When you are using the declarative JSON syntax:

  {
    "is-beta": "sitejet"
  }

The IsBeta rule expects a /var/cpanel/plugins/{$plugin}/config.yaml file && an'is-beta' section in yaml.

For ex: The is-beta check for 'sitejet' expects a '/var/cpanel/plugins/sitejet/config.yaml' file && an 'is-beta' section in yaml.

 is-beta:
 - company-id:
   - 7     # cpanel employees, dns only
   - 4393  # plesk test

=cut

=head1 METHODS

=over

=item * is_allowed -- Allow the plugin's beta features if rules are met.

    RETURNS
        0 - The is-beta check failed.
        1 - The is-beta check succeeded.

    ERRORS
        Fails if an invalid plugin is provided.
        Fails if the provided plugin is not extended for IsBeta check.

=back

=cut

around BUILDARGS => sub {
    my ( $orig, $class, @args ) = @_;

    # ( 1 )
    return { config => $args[0] }
      if @args == 1 && !ref $args[0];

    ## ( config => 1 ) ... ( config => [1] )
    my %args = @args;
    $args{config} = $args{config} if !ref $args{config};
    return $class->$orig(%args);
};

sub is_allowed ($self) {
    my $plugin = $self->config();

    # Validate the config passed
    die "You must provide a plugin."
      if !defined $plugin;

    # Run the rule
    my $config_file = _get_config_file($plugin);
    my $config      = eval { _read_config($config_file); };

    if ( $@ && $@ =~ /Please provide the YAML config file./ ) {
        logger()->warn("Could not find the '$config_file' for '$plugin'. Maybe IsBeta rule is not extended.");
        return !!0;
    }

    if ( !defined $config->{"is-beta"} ) {
        logger()->warn("The 'is-beta' section is missing in '$config_file' for plugin '$plugin'. Nothing to process further.");
        return !!0;
    }

    return Cpanel::Plugins::Components::Rules->new( config => $config->{"is-beta"} )->is_allowed();
}

sub _get_config_file ($plugin) {
    return "/var/cpanel/plugins/$plugin/config.yaml";
}

sub _read_config ($config_file) {
    require Common::YAML;
    return Common::YAML::load($config_file);
}

1;
