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

class RadioTinyCards 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'];

        $gridCfg = (isset($opts['grid']) && is_array($opts['grid'])) ? $opts['grid'] : [];
        $items = self::normalizeOptions($options, $value);
        $cards = '';

        foreach ($items as $item) {
            $valueEsc = self::h($item['value']);
            $optLabelEsc = self::h($item['label']);
            $checkedAttr = (string)$item['checked_attr'];
            $disabledAttr = (string)($item['disabled_attr'] ?? '');
            $ariaLabelEsc = self::h($item['aria_label']);

            $cards .= <<<HTML
<label aria-label="{$ariaLabelEsc}" class="group relative flex items-center justify-center rounded-md border border-gray-300 bg-white p-3 has-checked:border-brand has-checked:bg-brand has-focus-visible:outline-2 has-focus-visible:outline-offset-2 has-focus-visible:outline-brand has-disabled:border-gray-300 has-disabled:bg-gray-100 has-disabled:opacity-40 dark:border-white/20 dark:bg-gray-900 dark:has-checked:border-brand dark:has-checked:bg-brand dark:has-disabled:border-white/20 dark:has-disabled:bg-gray-800">
  <input type="radio" name="{$nameEsc}" value="{$valueEsc}" class="absolute inset-0 appearance-none focus:outline-none disabled:cursor-not-allowed"{$checkedAttr}{$disabledAttr}{$requiredAttr} {$extra} />
  <span class="text-sm font-medium text-gray-900 uppercase group-has-checked:text-white dark:text-white">{$optLabelEsc}</span>
</label>
HTML;
        }

        $gridClasses = self::buildTinyGridClasses($gridCfg);
        $containerClass = $ctx['container_class'];

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

    protected static function buildTinyGridClasses(array $gridCfg): string
    {
        $baseCols = (int)($gridCfg['base'] ?? 3);
        $smCols = (int)($gridCfg['sm'] ?? 6);
        $mdCols = (int)($gridCfg['md'] ?? 0);
        $lgCols = (int)($gridCfg['lg'] ?? 0);
        $xlCols = (int)($gridCfg['xl'] ?? 0);

        $classes = 'grid gap-3 grid-cols-' . max(1, $baseCols);
        if ($smCols > 0) { $classes .= ' sm:grid-cols-' . $smCols; }
        if ($mdCols > 0) { $classes .= ' md:grid-cols-' . $mdCols; }
        if ($lgCols > 0) { $classes .= ' lg:grid-cols-' . $lgCols; }
        if ($xlCols > 0) { $classes .= ' xl:grid-cols-' . $xlCols; }

        return $classes;
    }
}
