<?php
// components/Form/Select.php
namespace Components\Form;

class Select extends BaseField {
    /**
     * @param array $options key=>label pairs
     */
    public static function render(string $name, string $label, array $options = [], $value = '', array $opts = []): string {
        $errors = $opts['errors'] ?? [];
        $help = $opts['help'] ?? '';
        $extra = $opts['input_attrs'] ?? '';
        $hasError = !empty($errors);

        $nameEsc = self::h($name);
        $labelEsc = self::h($label);

        // Build id for label association
        $idBase = preg_replace('/[^a-zA-Z0-9_-]+/', '-', $name);
        $idBase = trim($idBase ?? '', '-');
        if ($idBase === '') { $idBase = 'select'; }
        $id = self::h($idBase);

        $includeEmpty = $opts['include_empty'] ?? true;
        $emptyLabel = $opts['empty_label'] ?? '-- Select --';
        $emptyValue = array_key_exists('empty_value', $opts) ? (string)$opts['empty_value'] : '';

        $optionsHtml = '';
        if ($includeEmpty) {
            $sel = ((string)$emptyValue === (string)$value) ? ' selected' : '';
            $emptyValEsc = self::h((string)$emptyValue);
            $emptyLabelEsc = self::h((string)$emptyLabel);
            $optionsHtml .= <<<HTML
<option value="{$emptyValEsc}"{$sel}>{$emptyLabelEsc}</option>
HTML;
        }
        foreach ($options as $val => $text) {
            $sel = ((string)$val === (string)$value) ? ' selected' : '';
            $valEsc = self::h((string)$val);
            $textEsc = self::h((string)$text);
            $optionsHtml .= <<<HTML
<option value="{$valEsc}"{$sel}>{$textEsc}</option>
HTML;
        }

        $helpHtml = self::helpHtml($help);
        $errorHtml = self::errorsHtml($errors);
        $clientErr = self::clientErrorHtml($name);
        $badge = $opts['label_badge_html'] ?? '';

        $labelHtml = '';
        if (!($opts['no_label'] ?? false)) {
            $labelCls = self::labelClass();
            $labelHtml = <<<HTML
<label for="{$id}" class="{$labelCls}">{$labelEsc}{$badge}</label>
HTML;
        }

        $inputWrapperClass = $opts['no_label'] ?? false ? '' : 'mt-2';
        $containerClass = (string)($opts['container_class'] ?? '');

        // Select base class; note appearance-none and extra right padding for chevron
        if (($opts['variant'] ?? '') === 'ghost') {
            $selectClass = 'col-start-1 row-start-1 appearance-none bg-transparent border-none rounded-md py-1.5 pr-8 pl-3 text-base sm:text-sm/6 focus:ring-0 cursor-pointer hover:bg-gray-50 dark:text-white dark:hover:bg-white/10';
        } else {
            $selectClass = self::inputClass($hasError);
            $selectClass = str_replace('block', 'col-start-1 row-start-1 appearance-none', $selectClass);
            $selectClass .= ' pr-8 pl-3 dark:*:bg-gray-800';
        }

        return <<<HTML
<div class="{$containerClass}">
{$labelHtml}
<div class="{$inputWrapperClass} grid grid-cols-1">

  <select id="{$id}" name="{$nameEsc}" class="{$selectClass}" {$extra}>
    {$optionsHtml}
  </select>
  <svg viewBox="0 0 16 16" fill="currentColor" data-slot="icon" aria-hidden="true" class="pointer-events-none col-start-1 row-start-1 mr-2 size-5 self-center justify-self-end text-gray-500 sm:size-4 dark:text-gray-400">
    <path d="M4.22 6.22a.75.75 0 0 1 1.06 0L8 8.94l2.72-2.72a.75.75 0 1 1 1.06 1.06l-3.25 3.25a.75.75 0 0 1-1.06 0L4.22 7.28a.75.75 0 0 1 0-1.06Z" clip-rule="evenodd" fill-rule="evenodd" />
  </svg>
</div>
{$helpHtml}
{$errorHtml}
{$clientErr}
</div>
HTML;
    }
}
