<?php

declare(strict_types=1);

namespace PpPhp\Plugins;

use PpPhp\Core\EditPlugin;
use PpPhp\Core\PluginSupport;

/**
 * Accessible star-rating input. Pure CSS (no JS): keyboard-navigable radio group
 * that renders as clickable stars with hover preview and an optional clear. Works
 * in the Designer preview and generated apps alike (the stylesheet ships via
 * assets()). Pair with the "Star Rating" VIEW control to display a saved value.
 */
class StarRating implements EditPlugin
{
    use PluginSupport;

    public static function id(): string { return 'StarRating'; }
    public static function label(): string { return 'Star Rating'; }
    public static function version(): string { return '2.0.0'; }
    public static function description(): string { return 'Accessible, keyboard-navigable star-rating input (pure CSS) with hover preview and optional clear.'; }

    public static function render(string $fieldName, mixed $value, array $settings): string
    {
        $max = (int) ($settings['max'] ?? 5);
        $max = max(1, min(10, $max));
        $cur = (int) round((float) ($value ?? 0));

        $name = self::e($fieldName);
        $base = 'ppstar_' . preg_replace('/[^A-Za-z0-9_]/', '_', $fieldName);
        $size = (string) ($settings['size'] ?? 'md');
        if (!in_array($size, ['sm', 'md', 'lg'], true)) $size = 'md';
        $allowClear = in_array(strtolower((string) ($settings['allowClear'] ?? 'yes')), ['1', 'yes', 'true', 'on'], true);

        $h = '<div class="pp-stars is-' . $size . '" role="radiogroup" aria-label="' . $name . ' rating">';
        // Reverse DOM order so the CSS "input:checked ~ .pp-star" trick lights the
        // selected star and every lower one (they follow in the source).
        for ($i = $max; $i >= 1; $i--) {
            $id = $base . '_' . $i;
            $checked = $cur === $i ? ' checked' : '';
            $h .= '<input class="pp-star-in" type="radio" name="' . $name . '" id="' . $id . '" value="' . $i . '"' . $checked . '>'
                . '<label class="pp-star" for="' . $id . '" title="' . $i . ' / ' . $max . '" aria-label="' . $i . ' star' . ($i > 1 ? 's' : '') . '">&#9733;</label>';
        }
        if ($allowClear) {
            $id = $base . '_0';
            $checked = $cur === 0 ? ' checked' : '';
            $h .= '<input class="pp-star-clear-in" type="radio" name="' . $name . '" id="' . $id . '" value="0"' . $checked . '>'
                . '<label class="pp-star-clear" for="' . $id . '" title="Clear" aria-label="Clear rating">&times;</label>';
        }
        $h .= '</div>';
        return $h;
    }

    public static function settingsSchema(): array
    {
        return [
            ['key' => 'max',       'type' => 'number', 'label' => 'Number of stars', 'default' => 5, 'hint' => '1–10.'],
            ['key' => 'size',      'type' => 'select', 'label' => 'Size', 'options' => ['sm', 'md', 'lg'], 'default' => 'md'],
            ['key' => 'allowClear', 'type' => 'bool',  'label' => 'Allow clearing to 0', 'default' => 'yes'],
        ];
    }

    public static function assets(): array
    {
        return ['js' => [], 'css' => ['plugins/Edit/StarRating/star-rating.css']];
    }
}
