<?php
namespace PpPhp\Plugins;
use PpPhp\Core\EditPlugin;
use PpPhp\Core\Designer\FieldControl\LookupOptions;

/**
 * Styled radio group — options from a manual list (plain or value:Label
 * pairs) OR looked up from a database table with an optional WHERE
 * condition; inline/stacked layout, size, color and disabled state, all
 * configurable from the Designer properties panel.
 */
class RadioGroup implements EditPlugin
{
    public static function id(): string { return 'RadioGroup'; }
    public static function label(): string { return 'Radio Group'; }
    public static function version(): string { return '1.0.0'; }
    public static function description(): string { return 'A set of radio buttons for a single choice from fixed options.'; }

    public static function render(string $fieldName, mixed $value, array $settings): string
    {
        $size = (string) ($settings['size'] ?? 'md');
        if (!in_array($size, ['sm', 'md', 'lg'], true)) $size = 'md';
        $colors = ['primary', 'info', 'success', 'danger', 'secondary', 'warning', 'dark'];
        $color = (string) ($settings['color'] ?? 'primary');
        if (!in_array($color, $colors, true)) $color = 'primary';
        $disabled = ($settings['state'] ?? '') === 'disabled';
        $stack = ($settings['layout'] ?? 'inline') === 'stacked';

        $options = LookupOptions::resolve($settings);
        if ($options === []) $options = ['Yes' => 'Yes', 'No' => 'No'];

        $html = '<div class="pp-ctl-group pp-sz-' . $size . ' pp-c-' . $color . ($stack ? ' pp-stack' : '') . '">';
        $i = 0;
        foreach ($options as $val => $label) {
            $valEsc = htmlspecialchars((string) $val);
            $checked = (string) $value !== '' && (string) $value === (string) $val;
            $html .= '<label class="pp-ctl pp-radio' . ($disabled ? ' pp-disabled' : '') . '">'
                . '<input type="radio" name="' . $fieldName . '"' . ($i === 0 ? ' id="field_' . $fieldName . '"' : '')
                . ' value="' . $valEsc . '"' . ($checked ? ' checked' : '') . ($disabled ? ' disabled' : '') . '>'
                . '<span class="pp-ctl-box"></span><span class="pp-ctl-txt">' . htmlspecialchars((string) $label) . '</span></label>';
            $i++;
        }

        return $html . '</div>';
    }

    public static function settingsSchema(): array
    {
        return array_merge(LookupOptions::schema(), [
            ['key' => 'layout', 'label' => 'Layout', 'type' => 'select', 'options' => ['inline', 'stacked'], 'default' => 'inline'],
            ['key' => 'size', 'label' => 'Size', 'type' => 'select', 'options' => ['sm', 'md', 'lg'], 'default' => 'md'],
            ['key' => 'color', 'label' => 'Color', 'type' => 'select', 'options' => ['primary', 'info', 'success', 'danger', 'secondary', 'warning', 'dark'], 'default' => 'primary'],
            ['key' => 'state', 'label' => 'State', 'type' => 'select', 'options' => ['normal', 'disabled'], 'default' => 'normal'],
        ]);
    }

    public static function assets(): array { return ['js' => [], 'css' => []]; }
}