<?php

declare(strict_types=1);

namespace Builder\Core\Foundation\Definition;

use Closure;

final class Hook
{
    /** @var Closure(mixed, array<string, mixed>): void */
    private readonly Closure $callback;

    /**
     * @param  Closure(mixed, array<string, mixed>): void  $callback
     */
    private function __construct(
        private readonly string $event,
        Closure $callback,
    ) {
        $this->callback = $callback;
    }

    /**
     * @param  Closure(mixed, array<string, mixed>): void  $callback
     */
    public static function on(string $event, Closure $callback): self
    {
        return new self($event, $callback);
    }

    public function getEvent(): string
    {
        return $this->event;
    }

    /** @param array<string, mixed> $data */
    public function execute(mixed $model, array $data): void
    {
        ($this->callback)($model, $data);
    }
}
