<?php

declare(strict_types=1);

namespace PpPhp\Plugins;

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

/**
 * From/to date range filter. Submits as f[column][from] / f[column][to] so the
 * generated list query applies col >= ? / col < (to + 1 day) — the upper bound
 * includes the whole 'to' day for DATE and DATETIME columns. Text inputs are
 * enhanced by the bundled flatpickr (themed calendar). Preserves the values.
 */
class DateRange implements FilterPlugin
{
    use PluginSupport;

    public static function id(): string { return 'DateRange'; }
    public static function label(): string { return 'Date Range'; }
    public static function version(): string { return '1.2.0'; }
    public static function description(): string { return 'Filter a date column by a from/to range (either side optional; the "to" day is included).'; }

    public static function render(string $fieldName, array $settings): string
    {
        $col = preg_replace('/[^A-Za-z0-9_]/', '', $fieldName);
        $fromVal = self::e($_GET['f'][$col]['from'] ?? '');
        $toVal   = self::e($_GET['f'][$col]['to'] ?? '');

        return '<span class="pp-daterange">'
            . '<input type="text" name="f[' . self::e($col) . '][from]" value="' . $fromVal . '" class="ppd-grid-search pp-input" data-pp-picker="date" placeholder="From…" autocomplete="off">'
            . '<span class="pp-daterange-sep">→</span>'
            . '<input type="text" name="f[' . self::e($col) . '][to]" value="' . $toVal . '" class="ppd-grid-search pp-input" data-pp-picker="date" placeholder="To…" autocomplete="off">'
            . '</span>';
    }

    public static function settingsSchema(): array { return []; }

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