<?php

declare(strict_types=1);

namespace PpPhp\Plugins;

use PpPhp\Core\PluginSupport;
use PpPhp\Core\ViewPlugin;

class Percent implements ViewPlugin
{
    use PluginSupport;

    public static function id(): string { return 'Percent'; }
    public static function label(): string { return 'Percent'; }
    public static function version(): string { return '1.1.0'; }
    public static function description(): string { return 'Displays a number as a percentage, with configurable decimals and whether the value is already a percent.'; }

    public static function render(mixed $value, array $settings): string
    {
        if ($value === null || $value === '') return '';
        $decimals = max(0, (int) ($settings['decimals'] ?? 0));
        // 'ratio' (default) treats 0.25 as 25%; 'whole' treats 25 as 25%.
        $mode = (string) ($settings['mode'] ?? 'ratio');
        $n = (float) $value * ($mode === 'whole' ? 1 : 100);
        return number_format($n, $decimals) . '%';
    }

    public static function settingsSchema(): array
    {
        return [
            ['key' => 'decimals', 'type' => 'number', 'label' => 'Decimal places', 'default' => 0],
            ['key' => 'mode', 'type' => 'select', 'label' => 'Stored value is', 'options' => ['ratio', 'whole'], 'default' => 'ratio',
             'hint' => 'ratio: 0.25 → 25% · whole: 25 → 25%.'],
        ];
    }

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