<?php

declare(strict_types=1);

namespace Duplicator\Addons\CapabilitiesAddon;

use Duplicator\Addons\CapabilitiesAddon\Core\LicenseCapabilities;
use Duplicator\Core\CapMng;

/**
 * Capabilities addon usage statistics contribution
 */
class CapabilitiesUsageStats
{
    /**
     * Register hooks
     *
     * @return void
     */
    public static function register(): void
    {
        add_filter('duplicator_usage_stats_plugin_data', [self::class, 'addPluginData'], 10, 2);
        add_filter('duplicator_usage_stats_sanitize_rules', [self::class, 'addSanitizeRules']);
    }

    /**
     * Add sanitize rules for capabilities-related stats fields
     *
     * @param array<string, string> $rules Existing rules
     *
     * @return array<string, string>
     */
    public static function addSanitizeRules(array $rules): array
    {
        $rules['can_use_adv_permissions']           = 'bool';
        $rules['have_custom_adv_permissions']       = 'bool';
        $rules['have_custom_users_adv_permissions'] = 'bool';

        return $rules;
    }

    /**
     * Add capabilities-related data to the plugin stats payload
     *
     * @param array<string, mixed> $data       Current stats data
     * @param mixed                $pluginData PluginData instance
     *
     * @return array<string, mixed>
     */
    public static function addPluginData(array $data, $pluginData): array
    {
        $data['can_use_adv_permissions']           = LicenseCapabilities::canPlus();
        $data['have_custom_adv_permissions']       = !CapMng::getInstance()->isDefault();
        $data['have_custom_users_adv_permissions'] = CapMng::getInstance()->hasUsersCapabilities();

        return $data;
    }
}
