<?php

namespace GriffinForms\Includes\CommandPalette;

use GriffinForms\Traits\Singleton;

class FormOptionsRegistry
{
    use Singleton;

    private array $entries = [];

    protected function setAll(): void
    {
        $this->entries = [
            // general tab
            ['option_id' => 'name',        'label' => __('Form Name', 'griffinforms-form-builder'),              'description' => __('The internal name used to identify this form.', 'griffinforms-form-builder'),                        'tab' => 'general'],
            ['option_id' => 'heading',      'label' => __('Heading', 'griffinforms-form-builder'),                'description' => __('The public-facing heading displayed above the form.', 'griffinforms-form-builder'),                   'tab' => 'general'],
            ['option_id' => 'description',  'label' => __('Description', 'griffinforms-form-builder'),            'description' => __('A short description shown below the form heading.', 'griffinforms-form-builder'),                     'tab' => 'general'],
            ['option_id' => 'folder',       'label' => __('Folder', 'griffinforms-form-builder'),                 'description' => __('The folder this form is grouped under.', 'griffinforms-form-builder'),                                'tab' => 'general'],
            // behavior tab
            ['option_id' => 'autofill',     'label' => __('Browser Autofill', 'griffinforms-form-builder'),       'description' => __('Allow browsers to autofill fields on this form.', 'griffinforms-form-builder'),                       'tab' => 'behavior'],
            ['option_id' => 'save_entries', 'label' => __('Save Entries', 'griffinforms-form-builder'),           'description' => __('Control whether form submissions are saved to the database.', 'griffinforms-form-builder'),           'tab' => 'behavior'],
            ['option_id' => 'user_action',  'label' => __('Post-Submission Action', 'griffinforms-form-builder'), 'description' => __('What happens after a user submits the form: message or redirect.', 'griffinforms-form-builder'),      'tab' => 'behavior'],
            ['option_id' => 'progress_bar', 'label' => __('Progress Bar', 'griffinforms-form-builder'),           'description' => __('Show a progress indicator on multi-page forms.', 'griffinforms-form-builder'),                        'tab' => 'behavior'],
            // availability tab
            ['option_id' => 'availability_window',   'label' => __('Availability Window', 'griffinforms-form-builder'), 'description' => __('Set start and end dates to control when the form is open.', 'griffinforms-form-builder'),     'tab' => 'availability'],
            ['option_id' => 'limit_entries',         'label' => __('Limit Entries', 'griffinforms-form-builder'),       'description' => __('Cap the number of submissions accepted.', 'griffinforms-form-builder'),                        'tab' => 'availability'],
            ['option_id' => 'access_level',          'label' => __('Access Level', 'griffinforms-form-builder'),        'description' => __('Restrict form visibility to logged-in users or specific roles.', 'griffinforms-form-builder'), 'tab' => 'availability'],
            // submission tab
            ['option_id' => 'submission_edit',           'label' => __('Submission Editing', 'griffinforms-form-builder'), 'description' => __('Allow submitters to return and edit their own entries.', 'griffinforms-form-builder'),     'tab' => 'submission'],
            ['option_id' => 'lifetime_submission_count', 'label' => __('Submission Count', 'griffinforms-form-builder'),  'description' => __('Total number of submissions received by this form.', 'griffinforms-form-builder'),          'tab' => 'submission'],
            // compliance tab
            ['option_id' => 'compliance_flags',  'label' => __('Data Privacy Mode', 'griffinforms-form-builder'),  'description' => __('Override the global data retention and storage settings for this form.', 'griffinforms-form-builder'), 'tab' => 'compliance'],
            ['option_id' => 'compliance_mode',   'label' => __('Compliance Mode', 'griffinforms-form-builder'),    'description' => __('Enable strict compliance mode to restrict what data is stored.', 'griffinforms-form-builder'),          'tab' => 'compliance'],
            ['option_id' => 'compliance_profile','label' => __('Compliance Profile', 'griffinforms-form-builder'), 'description' => __('Choose a compliance profile: standard, gdpr, or custom.', 'griffinforms-form-builder'),               'tab' => 'compliance'],
        ];
    }

    public function getAll(): array
    {
        return $this->entries;
    }

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

        $q = strtolower($query);

        return array_values(array_filter($this->entries, function (array $entry) use ($q): bool {
            return stripos($entry['label'], $q) !== false
                || stripos($entry['description'], $q) !== false;
        }));
    }
}
