<?php

declare(strict_types=1);

namespace apexl\Io\modules\validation\Hooks;

use apexl\Io\interfaces\AbstractHook;
use apexl\Io\modules\validation\PhpValidatorRules\ArrayRule;
use apexl\Io\modules\validation\PhpValidatorRules\DatetimeRule;
use apexl\Io\modules\validation\PhpValidatorRules\JsonValidatesRule;
use apexl\Io\modules\validation\PhpValidatorRules\Unique;
use apexl\Io\modules\validation\validationModule;
use BlakvGhost\PHPValidator\Contracts\Rule;
use BlakvGhost\PHPValidator\Mapping\RulesMaped;
use BlakvGhost\PHPValidator\ValidatorException;

final readonly class AddCustomPhpValidatorRules implements AbstractHook
{
    /** @var array<string, class-string<Rule>> */
    private const array RULES = [
        'array' => ArrayRule::class,
        'datetime' => DateTimeRule::class,
        'jsonvalidates' => JsonValidatesRule::class,
        'unique' => Unique::class,
    ];

    public function hooks(): array
    {
        return [validationModule::HOOK__CUSTOM_PHP_VALIDATOR_RULES];
    }

    public function __invoke(): void
    {
        foreach (self::RULES as $alias => $rule) {
            try {
                RulesMaped::addRule($alias, $rule);
            } catch (ValidatorException $e) {
                logger('default')->warning($e->getMessage());
            }
        }
    }
}
