<?php

namespace App\Domains\Webhook\Services;

use App\Domains\Webhook\Jobs\DeliverWebhookJob;
use App\Domains\Webhook\Models\Webhook;

class WebhookDispatcher
{
    /** @param array<string, mixed> $payload */
    public function dispatch(string $event, array $payload, int $workspaceId): void
    {
        Webhook::where('workspace_id', $workspaceId)
            ->where('is_active', true)
            ->whereJsonContains('events', $event)
            ->each(function (Webhook $webhook) use ($event, $payload) {
                DeliverWebhookJob::dispatch($webhook, $event, $payload)
                    ->onQueue('default');
            });
    }
}
