<?php

declare(strict_types=1);

namespace PpPhp\Plugins;

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

class Number implements ViewPlugin
{
    use PluginSupport;

    public static function id(): string { return 'Number'; }
    public static function label(): string { return 'Number'; }
    public static function version(): string { return '1.1.0'; }
    public static function description(): string { return 'Formats a numeric value with decimals, optional thousands separator, and prefix/suffix.'; }

    public static function render(mixed $value, array $settings): string
    {
        if ($value === null || $value === '') return '';
        $decimals = max(0, (int) ($settings['decimals'] ?? 0));
        $thousands = !in_array(strtolower((string) ($settings['thousands'] ?? 'yes')), ['0', 'no', 'false', 'off'], true);
        $num = number_format((float) $value, $decimals, '.', $thousands ? ',' : '');
        return self::e($settings['prefix'] ?? '') . $num . self::e($settings['suffix'] ?? '');
    }

    public static function settingsSchema(): array
    {
        return [
            ['key' => 'decimals',  'type' => 'number', 'label' => 'Decimal places', 'default' => 0],
            ['key' => 'thousands', 'type' => 'bool',   'label' => 'Thousands separator', 'default' => 'yes'],
            ['key' => 'prefix',    'type' => 'text',   'label' => 'Prefix', 'default' => '', 'hint' => 'e.g. $'],
            ['key' => 'suffix',    'type' => 'text',   'label' => 'Suffix', 'default' => '', 'hint' => 'e.g. kg'],
        ];
    }

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