<?php

declare(strict_types=1);

use Builder\Core\Foundation\Definition\Hook;

it('on creates a Hook instance', function (): void {
    $hook = Hook::on('saving', function (mixed $model, array $data): void {});

    expect($hook)->toBeInstanceOf(Hook::class);
});

it('getEvent returns the event name', function (): void {
    $hook = Hook::on('saved', function (mixed $model, array $data): void {});

    expect($hook->getEvent())->toBe('saved');
});

it('execute calls the callback with model and data', function (): void {
    $called = false;
    $receivedModel = null;
    $receivedData = null;

    $hook = Hook::on('saving', function (mixed $model, array $data) use (&$called, &$receivedModel, &$receivedData): void {
        $called = true;
        $receivedModel = $model;
        $receivedData = $data;
    });

    $model = new stdClass;
    $data = ['title' => 'Hello'];

    $hook->execute($model, $data);

    expect($called)->toBeTrue()
        ->and($receivedModel)->toBe($model)
        ->and($receivedData)->toBe($data);
});

it('supports multiple event types', function (): void {
    $events = ['saving', 'saved', 'creating', 'created', 'updating', 'updated', 'deleting', 'deleted'];

    foreach ($events as $event) {
        $hook = Hook::on($event, function (mixed $model, array $data): void {});
        expect($hook->getEvent())->toBe($event);
    }
});
