<?php

declare(strict_types=1);

namespace PpPhp\Plugins;

use PpPhp\Core\FilterPlugin;
use PpPhp\Core\PluginSupport;

/**
 * Free-text column filter. The match mode (contains / starts with / ends with /
 * exact) is honoured by the generated list query. Submits as f[column] so the
 * built-in filter WHERE reads it.
 */
class TextFilter implements FilterPlugin
{
    use PluginSupport;

    public static function id(): string { return 'TextFilter'; }
    public static function label(): string { return 'Text Filter'; }
    public static function version(): string { return '1.1.0'; }
    public static function description(): string { return 'A text search box with a configurable match mode (contains / starts / ends / exact).'; }

    public static function render(string $fieldName, array $settings): string
    {
        $name = 'f[' . $fieldName . ']';
        $cur  = self::e($_GET['f'][$fieldName] ?? '');
        return '<input type="text" name="' . self::e($name) . '" value="' . $cur . '" class="ppd-grid-search pp-input" placeholder="Search ' . self::e($fieldName) . '">';
    }

    public static function settingsSchema(): array
    {
        return [
            ['key' => 'mode', 'type' => 'select', 'label' => 'Match', 'options' => ['contains', 'starts', 'ends', 'exact'], 'default' => 'contains',
             'hint' => 'contains: anywhere · starts/ends: prefix/suffix · exact: full value.'],
        ];
    }

    public static function assets(): array { return ['js' => [], 'css' => []]; }
}
