<?php
// components/Ui/FilterPills.php
namespace Components\Ui;

use Components\Button;

class FilterPills
{
    /**
     * Render a set of filter pills consisting of an optional "ALL" pill and one pill per provided filter value.
     *
     * Expects Alpine helpers to be present in the surrounding scope by default:
     *   - setFilter(v)
     *   - isFilter(v)
     * You can change the function names using opts: isFilterFn, setFilterFn.
     *
     * @param array $filters List of filter values (strings) to render as pills
     * @param array $opts Options:
     *  - include_all: bool (default true)
     *  - all_label: string (default 'ALL') — overridden by labels_map['ALL'] when present
     *  - labels_map: array<string, string> Mapping from filter value to display label (supports 'ALL' key)
     *  - isFilterFn: string (default 'isFilter')
     *  - setFilterFn: string (default 'setFilter')
     *  - active_class: string (default 'bg-brand/50! text-white!') // applied conditionally via :class
     *  - button_opts: array // forwarded to Button::render (variant/size/rounded/class/attrs merged)
     */
    public static function render(array $filters, array $opts = []): string
    {
        $includeAll  = $opts['include_all'] ?? true;
        $labelsMap   = is_array($opts['labels_map'] ?? null) ? $opts['labels_map'] : [];
        $allLabel    = (string)($labelsMap['ALL'] ?? ($opts['all_label'] ?? mh_lng('MAILBEEZ_UI_FILTER_ALL_LABEL', 'All')));
        $isFilterFn  = (string)($opts['isFilterFn'] ?? 'isFilter');
        $setFilterFn = (string)($opts['setFilterFn'] ?? 'setFilter');
        $activeClass = (string)($opts['active_class'] ?? 'bg-brand/70! text-white/90!');
        $buttonOpts  = is_array($opts['button_opts'] ?? null) ? $opts['button_opts'] : [];

        // Defaults aligned with existing usage
        $defaultBtn = [
            'variant' => 'secondary',
            'size'    => 'sm',
            'rounded' => 'full',
            'class'   => 'aria-pressed:bg-gray-100 aria-pressed:text-secondary px-3!',
            'attrs'   => [],
        ];

        // Merge provided overrides shallowly
        $btnBase = array_merge($defaultBtn, $buttonOpts);
        $btnBase['attrs'] = array_merge($defaultBtn['attrs'], $buttonOpts['attrs'] ?? []);

        ob_start();
        if ($includeAll) {
            $attrs = array_merge($btnBase['attrs'], [
                '@click'       => $setFilterFn . "('ALL')",
                ':class'       => $isFilterFn . "('ALL') ? '" . $activeClass . "' : ''",
                'aria-pressed' => $isFilterFn . "('ALL') ? 'true' : 'false'",
            ]);
            echo Button::render($allLabel, array_merge($btnBase, ['attrs' => $attrs]));
        }

        foreach ($filters as $f) {
            // Ensure proper JSON encoding for JavaScript literal
            $js = json_encode($f, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            $attrs = array_merge($btnBase['attrs'], [
                '@click'       => $setFilterFn . '(' . $js . ')',
                ':class'       => $isFilterFn . '(' . $js . ") ? '" . $activeClass . "' : ''",
                'aria-pressed' => $isFilterFn . '(' . $js . ") ? 'true' : 'false'",
            ]);
            $label = (string)($labelsMap[(string)$f] ?? (string)$f);
            echo Button::render($label, array_merge($btnBase, ['attrs' => $attrs]));
        }

        return ob_get_clean();
    }
}
