<?php

namespace GriffinForms\Includes\CommandPalette\Providers;

use GriffinForms\Includes\CommandPalette\SearchProviderInterface;
use GriffinForms\Includes\CommandPalette\SearchResultItem;

class PluginSettingsProvider implements SearchProviderInterface
{
    private function getEntries(): array
    {
        return [
            ['tab' => 'general',          'label' => __('General Settings', 'griffinforms-form-builder'),  'description' => __('Global plugin options, date formats, and default behaviour.', 'griffinforms-form-builder')],
            ['tab' => 'ai',               'label' => __('AI / Griffin Assist', 'griffinforms-form-builder'), 'description' => __('Configure AI-powered form building and field suggestions.', 'griffinforms-form-builder')],
            ['tab' => 'compliance',       'label' => __('Compliance', 'griffinforms-form-builder'),         'description' => __('Global GDPR, data retention, and privacy settings.', 'griffinforms-form-builder')],
            ['tab' => 'mail',             'label' => __('Mail / SMTP', 'griffinforms-form-builder'),        'description' => __('Configure the outgoing email server and SMTP credentials.', 'griffinforms-form-builder')],
            ['tab' => 'payments',         'label' => __('Payments', 'griffinforms-form-builder'),           'description' => __('Payment gateway keys, currency, and checkout settings.', 'griffinforms-form-builder')],
            ['tab' => 'capabilitymatrix', 'label' => __('Capability Matrix', 'griffinforms-form-builder'),  'description' => __('Control which WordPress roles can access GriffinForms features.', 'griffinforms-form-builder')],
            ['tab' => 'antispam',         'label' => __('Spam Protection', 'griffinforms-form-builder'),    'description' => __('Honeypot, reCAPTCHA, and other anti-spam configuration.', 'griffinforms-form-builder')],
            ['tab' => 'filestorage',      'label' => __('File Storage', 'griffinforms-form-builder'),       'description' => __('Set storage provider and limits for file upload fields.', 'griffinforms-form-builder')],
            ['tab' => 'license',          'label' => __('License', 'griffinforms-form-builder'),            'description' => __('Manage your GriffinForms Pro license key.', 'griffinforms-form-builder')],
        ];
    }

    public function getProviderId(): string { return 'plugin_settings'; }
    public function getLabel(): string      { return 'Settings'; }
    public function getPriority(): int      { return 60; }
    public function getQuickItems(int $form_id): array { return []; }

    public function query(string $query, int $form_id): array
    {
        if ($query === '') {
            return [];
        }

        $results = [];

        foreach ($this->getEntries() as $entry) {
            if (stripos($entry['label'], $query) === false
                && stripos($entry['description'], $query) === false
                && stripos($entry['tab'], $query) === false
            ) {
                continue;
            }

            $url = admin_url('admin.php?page=griffinforms-settings-' . $entry['tab']);

            $results[] = new SearchResultItem(
                result_id:      'setting_' . $entry['tab'],
                provider_id:    $this->getProviderId(),
                label:          $entry['label'],
                action_type:    'navigate_url_new_tab',
                action_payload: ['url' => $url],
                description:    $entry['description'],
                icon:           'setting',
                score:          0
            );
        }

        return $results;
    }
}
