<?php

namespace GriffinForms\Includes\CommandPalette\Providers;

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

class ThemeActionsProvider implements SearchProviderInterface
{
    public function getProviderId(): string { return 'theme_actions'; }
    public function getLabel(): string      { return 'Theme Actions'; }
    public function getPriority(): int      { return 36; }
    public function getQuickItems(int $form_id): array { return []; }

    private function actions(): array
    {
        return [
            [
                'result_id'   => 'theme_action_remove',
                'label'       => __('Remove Theme', 'griffinforms-form-builder'),
                'description' => __('Remove the applied theme and revert to default styling', 'griffinforms-form-builder'),
                'keywords'    => ['remove theme', 'remove', 'untheme', 'reset theme', 'clear theme', 'delete theme'],
            ],
            [
                'result_id'   => 'theme_action_default',
                'label'       => __('Apply Default Theme', 'griffinforms-form-builder'),
                'description' => __('Apply the built-in Bootstrap styling without any custom theme overrides', 'griffinforms-form-builder'),
                'keywords'    => ['default theme', 'default style', 'default styling', 'bootstrap theme', 'no theme', 'use default'],
            ],
            [
                'result_id'   => 'theme_action_default_style',
                'label'       => __('Default Style', 'griffinforms-form-builder'),
                'description' => __('Revert to built-in Bootstrap styling with no custom theme', 'griffinforms-form-builder'),
                'keywords'    => ['default style', 'default styling', 'built-in', 'stock style', 'plain style', 'revert theme'],
            ],
        ];
    }

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

        $results = [];

        foreach ($this->actions() as $action) {
            $matched = stripos($action['label'], $query) !== false
                || stripos($action['description'], $query) !== false;

            if (!$matched) {
                foreach ($action['keywords'] as $kw) {
                    if (stripos($kw, $query) !== false) {
                        $matched = true;
                        break;
                    }
                }
            }

            if (!$matched) {
                continue;
            }

            $results[] = new SearchResultItem(
                result_id:      $action['result_id'],
                provider_id:    $this->getProviderId(),
                label:          $action['label'],
                action_type:    'apply_theme',
                action_payload: ['theme_id' => 0],
                description:    $action['description'],
                icon:           'theme_action',
                score:          0
            );
        }

        return $results;
    }
}
