<?php
/**
 * Scheduler audit outcome dispatch.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Scheduler;

use ScheduledChanges\Domain\Audit\AuditEvent;
use ScheduledChanges\Domain\Shared\UtcTimestamp;
use ScheduledChanges\Domain\Shared\Uuid;
use ScheduledChanges\Repository\AuditRepository;

/** Owns non-blocking lifecycle observations emitted by the queue worker. */
final class SchedulerOutcomeDispatcher {
	private AuditRepository $audit;

	public function __construct( AuditRepository $audit ) {
		$this->audit = $audit;
	}

	public function record( AuditEvent $event ): void {
		$this->audit->record( $event );
	}

	/**
	 * Records one scheduler audit event.
	 *
	 * @param array<string,mixed> $context Event context.
	 */
	public function audit( string $event_type, string $severity, int $launch_id, int $run_id, int $actor_id, array $context ): void {
		$this->record(
			new AuditEvent(
				Uuid::generate(),
				$launch_id,
				$run_id,
				$actor_id,
				$event_type,
				$severity,
				$context,
				UtcTimestamp::now()
			)
		);
	}
}
