<?php

namespace GriffinForms\Includes\CommandPalette\Providers;

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

class NavigationProvider implements SearchProviderInterface
{
    public function getProviderId(): string { return 'navigation'; }
    public function getLabel(): string      { return 'Pages'; }
    public function getPriority(): int      { return 15; }

    public function getQuickItems(int $form_id): array
    {
        return $this->buildItems($form_id, '');
    }

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

    private function buildItems(int $form_id, string $query): array
    {
        $entries = [
            [
                'label'       => __('Forms', 'griffinforms-form-builder'),
                'description' => __('View all your forms', 'griffinforms-form-builder'),
                'icon'        => 'nav_forms',
                'keywords'    => ['forms', 'list', 'all forms'],
                'url'         => admin_url('admin.php?page=griffinforms-forms'),
            ],
            [
                'label'       => __('Submissions', 'griffinforms-form-builder'),
                'description' => $form_id > 0 ? __('All submissions for this form', 'griffinforms-form-builder') : __('All submissions', 'griffinforms-form-builder'),
                'icon'        => 'nav_submissions',
                'keywords'    => ['submissions', 'entries', 'responses'],
                'url'         => $form_id > 0
                    ? admin_url('admin.php?page=griffinforms-submissions&form_id=' . $form_id . '&paged=1')
                    : admin_url('admin.php?page=griffinforms-submissions'),
            ],
            [
                'label'       => __('Unread Submissions', 'griffinforms-form-builder'),
                'description' => $form_id > 0 ? __('Unread submissions for this form', 'griffinforms-form-builder') : __('All unread submissions', 'griffinforms-form-builder'),
                'icon'        => 'nav_unread',
                'keywords'    => ['unread', 'new', 'unseen', 'submissions'],
                'url'         => $form_id > 0
                    ? admin_url('admin.php?page=griffinforms-submissions&form_id=' . $form_id . '&is_read=0&paged=1')
                    : admin_url('admin.php?page=griffinforms-submissions&is_read=0'),
            ],
            [
                'label'       => __('Attachments', 'griffinforms-form-builder'),
                'description' => __('Files uploaded through your forms', 'griffinforms-form-builder'),
                'icon'        => 'nav_files',
                'keywords'    => ['attachments', 'files', 'uploads', 'file uploads'],
                'url'         => admin_url('admin.php?page=griffinforms-files'),
            ],
            [
                'label'       => __('Messages', 'griffinforms-form-builder'),
                'description' => __('Email message templates', 'griffinforms-form-builder'),
                'icon'        => 'nav_messages',
                'keywords'    => ['messages', 'templates', 'email templates'],
                'url'         => admin_url('admin.php?page=griffinforms-messages'),
            ],
            [
                'label'          => __('Themes', 'griffinforms-form-builder'),
                'description'    => __('Manage form visual themes', 'griffinforms-form-builder'),
                'icon'           => 'nav_themes',
                'keywords'       => ['themes', 'form themes', 'styles', 'design'],
                'action_type'    => 'open_modal',
                'action_payload' => ['modal_id' => 'griffinforms-builder-modal-formthemes'],
            ],
            [
                'label'       => __('Logs', 'griffinforms-form-builder'),
                'description' => __('Activity and error logs', 'griffinforms-form-builder'),
                'icon'        => 'nav_logs',
                'keywords'    => ['logs', 'activity', 'errors', 'debug'],
                'url'         => admin_url('admin.php?page=griffinforms-logs'),
            ],
        ];

        $results = [];
        foreach ($entries as $i => $entry) {
            if ($query !== '') {
                $matched = stripos($entry['label'], $query) !== false
                    || stripos($entry['description'], $query) !== false;
                if (!$matched) {
                    foreach ($entry['keywords'] as $kw) {
                        if (stripos($kw, $query) !== false) {
                            $matched = true;
                            break;
                        }
                    }
                }
                if (!$matched) {
                    continue;
                }
            }

            $action_type    = $entry['action_type']    ?? 'navigate_url_new_tab';
            $action_payload = $entry['action_payload'] ?? ['url' => $entry['url'] ?? ''];

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

        return $results;
    }
}
