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

/**
 * RadioGroupInline
 * Renders a set of radio buttons in an inline layout.
 * Optimized for fewer options where TabsGroup might be too prominent.
 */
class RadioGroupInline extends RadioBase
{
    /**
     * Render the component as a form field.
     *
     * @param string $name
     * @param string $label
     * @param array $options
     * @param mixed $value
     * @param array $opts
     * @return string
     */
    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);
        $inputs = '';
        $index = 0;

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

            $idRaw = self::slugify($name) . '-' . $index;
            $idEsc = self::h($idRaw);

            $inputs .= <<<HTML
    <div class="flex items-center">
      <input id="{$idEsc}" type="radio" name="{$nameEsc}" value="{$valueEsc}" 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}{$disabledAttr}{$requiredAttr} {$extra} />
      <label for="{$idEsc}" class="ml-3 block text-sm/6 font-medium text-gray-900 dark:text-white">{$optLabelEsc}</label>
    </div>
HTML;
            $index++;
        }

        $layout = (string)($opts['layout'] ?? 'stacked');
        $isHorizontal = self::isHorizontalLayout($layout);

        $explainerHtml = '';
        $xModelVar = $opts['model'] ?? "form['" . addslashes($name) . "']";
        foreach ($items as $item) {
            if ($item['description'] !== '') {
                $valJs = json_encode((string)$item['value'], JSON_UNESCAPED_UNICODE);
                $descEsc = self::h($item['description']);
                $showExpr = htmlspecialchars("{$xModelVar} === {$valJs}", ENT_QUOTES);
                $explainerHtml .= <<<HTML
<div x-show="{$showExpr}" class="mt-2 text-sm text-gray-500 dark:text-gray-400">
    {$descEsc}
</div>
HTML;
            }
        }

        $innerHtml = <<<HTML
<div class="space-y-6 sm:flex sm:items-center sm:space-y-0 sm:space-x-10">
  {$inputs}
</div>
{$explainerHtml}
HTML;

        if (!$isHorizontal) {
            $containerClass = $ctx['container_class'];
            if ($layout === 'short') {
                $containerClass .= ' w-full md:w-1/2';
            }
            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">
    {$innerHtml}
  </div>
  {$errorHtml}
  {$clientErr}
</fieldset>
HTML;
        }

        $labelHtml = self::renderLabel('', $label, $badge, $opts);
        return self::renderContainer($labelHtml, $innerHtml, $helpHtml, $errorHtml, $clientErr, $opts);
    }
}
