<?php
namespace PpPhp\Plugins;

use PpPhp\Core\ValidationPlugin;

class EmailRule implements ValidationPlugin
{
    public static function id(): string { return 'email'; }
    public static function label(): string { return 'Email address'; }
    public static function group(): string { return 'Format'; }
    public static function paramsSchema(): array { return []; }
    public static function appliesTo(): array { return ['email', 'text']; }
    public static function defaultMessage(): string { return 'Enter a valid email address.'; }

    public static function serverCheck(mixed $value, array $params, array $record = []): bool
    {
        return filter_var(trim((string) $value), FILTER_VALIDATE_EMAIL) !== false;
    }

    public static function clientRule(array $params): array
    {
        // Pragmatic HTML-spec-ish email pattern (mirrors the browser's own).
        return ['pattern' => "^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$"];
    }
}
