<?php
namespace PpPhp\Plugins;

use PpPhp\Core\ValidationPlugin;

class DifferentRule implements ValidationPlugin
{
    public static function id(): string { return 'different'; }
    public static function label(): string { return 'Different from 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. current_password'],
        ];
    }

    public static function appliesTo(): array { return []; }
    public static function defaultMessage(): string { return 'Must be different from {field}.'; }

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

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