<?php

namespace App\Modules\Operations\Services;

use App\Modules\Operations\Repositories\OperationalControlEventRepository;

final class OperationalSignalRecorder
{
    public function __construct(
        private readonly OperationalControlEventRepository $events,
    ) {}

    /**
     * @param  array<string, mixed>  $meta
     */
    public function record(
        int $tenantId,
        string $channel,
        string $category,
        string $severity,
        string $eventCode,
        ?string $summary,
        ?string $correlationId,
        ?string $referenceType,
        ?int $referenceId,
        array $meta = [],
    ): void {
        if (! config('operations-control.persistence_enabled', true) || $tenantId <= 0) {
            return;
        }

        $this->events->insert([
            'tenant_id' => $tenantId,
            'channel' => $channel,
            'category' => $category,
            'severity' => $severity,
            'event_code' => $eventCode,
            'summary' => $summary,
            'correlation_id' => $correlationId,
            'reference_type' => $referenceType,
            'reference_id' => $referenceId,
            'meta' => $meta === [] ? null : $meta,
        ]);
    }
}
