<?php

declare(strict_types=1);

namespace PpPhp\Plugins;

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

class EmailHyperlink implements ViewPlugin
{
    use PluginSupport;

    public static function id(): string { return 'EmailHyperlink'; }
    public static function label(): string { return 'Email Link'; }
    public static function version(): string { return '1.1.0'; }
    public static function description(): string { return 'Renders an email address as a mailto: link.'; }

    public static function render(mixed $value, array $settings): string
    {
        $email = trim((string) ($value ?? ''));
        if ($email === '') return '';
        // Not a plausible address → show as plain text rather than a broken mailto.
        if (!str_contains($email, '@') || preg_match('/[\s<>"\']/', $email)) {
            return self::e($email);
        }
        return '<a href="mailto:' . self::e($email) . '">' . self::e($email) . '</a>';
    }

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