package Cpanel::UpdateGatherer::modules::WordPress;

#                                      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.



use Cpanel::Config::Users          ();
use Cpanel::Features::Check        ();
use Cpanel::Features               ();
use Cpanel::Features::Lists        ();
use Cpanel::Config::userdata::Load ();
use Cpanel::Server::Type           ();

use Cpanel::UpdateGatherer::Std;

use constant {
    WPT_KEY        => 'wp-toolkit',
    WPT_DELUXE_KEY => 'wp-toolkit-deluxe'
};

sub compile ( $self, $meta ) {
    _add_number_of_accounts_with_wpt_assigned_but_not_deluxe_to_metadata($meta);
    _add_wp2_features_metrics($meta);
    return 1;
}

sub _add_number_of_accounts_with_wpt_assigned_but_not_deluxe_to_metadata ($meta) {

    my $count_users_with_wpt_but_not_wptd = 0;
    my $count_users_with_wpt_and_wptd     = 0;
    my $is_wptk_installed                 = eval {
        require Whostmgr::PleskWordPressToolkit;
        Whostmgr::PleskWordPressToolkit::is_installed();
    };

    if ($is_wptk_installed) {
        my @users = Cpanel::Config::Users::getcpusers();

        foreach my $user (@users) {
            my $has_wptd = Cpanel::Features::Check::check_feature_for_user( $user, WPT_DELUXE_KEY );
            my $has_wpt  = Cpanel::Features::Check::check_feature_for_user( $user, WPT_KEY );

            $count_users_with_wpt_but_not_wptd++ if $has_wpt && !$has_wptd;
            $count_users_with_wpt_and_wptd++     if $has_wpt && $has_wptd;
        }
    }

    $meta->{'wp_toolkit_assigned'}            = $count_users_with_wpt_but_not_wptd;
    $meta->{'wp_toolkit_and_deluxe_assigned'} = $count_users_with_wpt_and_wptd;

    return 1;
}

sub _is_wp2() {
    my $is_wp2 = eval { Cpanel::Server::Type->can('is_wp_squared') } or return;
    return $is_wp2->();
}

sub _add_wp2_features_metrics ($meta) {

    # The features we want to track are WP2 specific - they don't exist on non-wp2 servers.
    return unless _is_wp2();
    my %feature_desc = map { $_->[0] => $_->[1] } Cpanel::Features::load_feature_descs();
    my %wp2_features = map {
        $_ => {
            name                            => $_,
            description                     => $feature_desc{$_} // 'Unknown or unavailable feature',
            featurelists_enabled            => 0,
            featurelists_disabled           => 0,
            disabled_in_default_featurelist => 0,
            disabled_globally               => 0,
            used_by_users                   => 0,                                                       # The number of users on the server configured to use featurelists with the feature enabled
            used_by_websites                => 0,                                                       # The number of websites (aka wptk instances) configured on users with the feature enabled
        }
    } qw{ sitecloning sitestaging cdn };

    my $globally_disabled_features = Cpanel::Features::load_featurelist('disabled') // {};
    my @feature_lists_on_server    = Cpanel::Features::Lists::get_feature_lists();

    my @non_globally_disabled_features;
    foreach my $wp_feature ( keys %wp2_features ) {
        if ( exists $globally_disabled_features->{$wp_feature} && !$globally_disabled_features->{$wp_feature} ) {
            $wp2_features{$wp_feature}->{disabled_globally} = 1;
        }
        else {
            push @non_globally_disabled_features, $wp_feature;
        }
    }

    foreach my $feature_list (@feature_lists_on_server) {

        next if $feature_list eq 'Mail Only';    # 'Mail Only' is a special feature list that has all non-mail features disabled.
        next if $feature_list eq 'disabled';     # 'disabled' is a special feature list that tracks 'globally disabled' features.

        my $list = Cpanel::Features::load_featurelist($feature_list);
        foreach my $wp_feature (@non_globally_disabled_features) {
            if ( exists $list->{$wp_feature} && !$list->{$wp_feature} ) {
                $wp2_features{$wp_feature}->{featurelists_disabled}++;
                $wp2_features{$wp_feature}->{disabled_in_default_featurelist} = 1 if $feature_list eq 'default';
            }
            else {
                $wp2_features{$wp_feature}->{featurelists_enabled}++;
            }
        }
    }

    foreach my $user ( Cpanel::Config::Users::getcpusers() ) {
        my $user_website_count = eval { scalar Cpanel::Config::userdata::Load::get_all_wptkids_for_user($user)->@* } // 0;
        foreach my $wp_feature (@non_globally_disabled_features) {
            if ( Cpanel::Features::Check::check_feature_for_user( $user, $wp_feature ) ) {
                $wp2_features{$wp_feature}->{used_by_users}++;
                $wp2_features{$wp_feature}->{used_by_websites} += $user_website_count;
            }
        }
    }

    $meta->{'used-features'} //= [];
    push $meta->{'used-features'}->@*, values %wp2_features;

    return 1;
}

1;
