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

class RadioList extends RadioBase
{
    public static function render(string $name, string $label, array $options, $value = '', array $opts = []): string
    {
        $ctx = self::buildRadioFieldContext($name, $label, $opts);

        $nameEsc = $ctx['name_esc'];
        $labelEsc = $ctx['label_esc'];
        $requiredAttr = $ctx['required_attr'];
        $groupErrorClass = $ctx['group_error_class'];
        $helpHtml = $ctx['help_html'];
        $errorHtml = $ctx['error_html'];
        $clientErr = $ctx['client_err_html'];
        $badge = $ctx['badge_html'];
        $extra = $ctx['extra_attrs'];

        $items = self::normalizeOptions($options, $value);
        $rows = '';
        $index = 0;

        foreach ($items as $item) {
            $valueEsc = self::h($item['value']);
            $optLabelEsc = self::h($item['label']);
            $optDescEsc = self::h($item['description']);
            $optMetaEsc = self::h($item['meta']);
            $checkedAttr = (string)$item['checked_attr'];
            $ariaLabelEsc = self::h($item['aria_label']);

            $idRaw = self::slugify($name) . '-' . $index;
            $idEsc = self::h($idRaw);
            $descId = $idRaw . '-description';
            $descIdEsc = self::h($descId);
            $hasDescription = ($item['description'] !== '');
            $describedBy = $hasDescription ? ' aria-describedby="' . $descIdEsc . '"' : '';

            $iconHtml = self::renderOptionIcon(
                (string)$item['icon'],
                (string)$item['token'],
                'mr-1 shrink-0 inline-flex size-6 items-center justify-center rounded text-white',
                '[&_svg]:size-4'
            );

            $descriptionHtml = $hasDescription
                ? '<p id="' . $descIdEsc . '" class="text-gray-500 dark:text-gray-400">' . $optDescEsc . '</p>'
                : '';

            $metaHtml = ($item['meta'] !== '')
                ? '<p class="text-xs text-gray-500 dark:text-gray-400 mt-1">' . $optMetaEsc . '</p>'
                : '';

            $rows .= <<<HTML
<div class="relative flex items-start">
  <div class="flex h-6 items-center">
    <input id="{$idEsc}" type="radio" name="{$nameEsc}" value="{$valueEsc}" aria-label="{$ariaLabelEsc}"{$describedBy} class="relative size-4 appearance-none rounded-full border border-gray-300 bg-white before:absolute before:inset-1 before:rounded-full before:bg-white not-checked:before:hidden checked:border-brand checked:bg-brand focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-brand disabled:border-gray-300 disabled:bg-gray-100 disabled:before:bg-gray-400 forced-colors:appearance-auto forced-colors:before:hidden dark:border-white/20 dark:bg-gray-900 dark:checked:border-brand dark:checked:bg-brand"{$checkedAttr}{$requiredAttr} {$extra} />
  </div>
  <div class="ml-3 text-sm/6">
    <label for="{$idEsc}" class="font-medium text-gray-900 dark:text-white inline-flex items-center">{$iconHtml}<span>{$optLabelEsc}</span></label>
    {$descriptionHtml}
    {$metaHtml}
  </div>
</div>
HTML;

            $index++;
        }

        return <<<HTML
<fieldset class="form-field{$groupErrorClass}">
  <legend class="mb-1 block text-sm font-medium text-primary">{$labelEsc} {$badge}</legend>
  {$helpHtml}
  <div class="space-y-5 mt-2">{$rows}</div>
  {$errorHtml}
  {$clientErr}
</fieldset>
HTML;
    }
}
