<?php

namespace Planaday\Utility\Debug\Log;

use Planaday\Utility\DataGrouping\UUIDGenerator;
use Planaday\Utility\DependencyInjection\Attributes\Singleton;

#[Singleton]
class Logger {
	public bool $logDebug = true;

	/** @var LogEntry[] */
	protected array $logs = [];

	/** @var LogListener[] */
	protected array $logListeners = [];

	protected string $requestId;

	public function __construct(){
		$this->requestId = UUIDGenerator::generateUUIDv4OrRandom();
	}

	/**
	 * @param LogEntry $log
	 *
	 * @return void
	 */
	public function log(LogEntry $log): void{
		$log->setRequestId($this->requestId);
		$this->logs[] = $log;
		foreach ($this->logListeners as $listener) {
			$listener->handleLog($log);
		}
	}

	public function addLogListener(LogListener $listener): void{
		$this->logListeners[] = $listener;
		foreach ($this->logs as $log) {
			$listener->handleLog($log);
		}
	}

	/**
	 * @return LogEntry[]
	 */
	public function getLogs(): array {
		return $this->logs;
	}
}