<?php
namespace PpPhp\Plugins;

use PpPhp\Core\ValidationPlugin;

class MatchRule implements ValidationPlugin
{
    public static function id(): string { return 'match'; }
    public static function label(): string { return 'Matches another field'; }
    public static function group(): string { return 'Cross-field'; }

    public static function paramsSchema(): array
    {
        return [
            ['key' => 'field', 'type' => 'text', 'label' => 'Other field name', 'default' => '', 'placeholder' => 'e.g. password'],
        ];
    }

    public static function appliesTo(): array { return []; }
    public static function defaultMessage(): string { return 'Does not match {field}.'; }

    public static function serverCheck(mixed $value, array $params, array $record = []): bool
    {
        $other = (string) ($params['field'] ?? '');
        if ($other === '') return true;
        return (string) $value === (string) ($record[$other] ?? '');
    }

    public static function clientRule(array $params): array
    {
        $other = (string) ($params['field'] ?? '');
        return $other === '' ? [] : ['match' => $other];
    }
}
