<?php

namespace Planaday\Utility\Debug\Log;

use DateTime;

class LogEntry {
	protected string $requestId;
	protected string $type;
	protected string $class;
	protected string $message;
	protected float $rawTime;
	protected float $relativeTime = 0;
	protected string $time;
	protected mixed $data;
	protected string $scopeId = "";

	public function getScopeId(): string {
		return $this->scopeId;
	}

	public function setScopeId( string $scopeId ): static {
		$this->scopeId = $scopeId;

		return $this;
	}
	public function setType( string $type ): static {
		$this->type = $type;

		return $this;
	}

	public function setClass( string $class ): static {
		$this->class = $class;

		return $this;
	}

	public function setMessage( string $message ): static {
		$this->message = $message;

		return $this;
	}

	public function setRawTime( float $rawTime ): static {
		$this->rawTime = $rawTime;

		return $this;
	}

	public function setTime( string $time ): static {
		$this->time = $time;

		return $this;
	}

	public function setData( mixed $data ): static {
		$this->data = $data;

		return $this;
	}

	public function setRequestId( string $requestId ): static {
		$this->requestId = $requestId;
		return $this;
	}

	public function getType(): string {
		return $this->type;
	}

	public function getClass(): string {
		return $this->class;
	}

	public function getMessage(): string {
		return $this->message;
	}

	public function getRawTime(): float {
		return $this->rawTime;
	}

	public function getRelativeTime(): float {
		return $this->relativeTime;
	}

	public function getRequestId(): string {
		return $this->requestId;
	}

	public function setRelativeTime(float $relativeTime): void {
		$this->relativeTime = $relativeTime;
	}

	public function getTime(): string {
		return $this->time;
	}

	public function getData(): mixed {
		return $this->data;
	}

	public function clearData(): void {
		$this->data = null;
	}

	public function getAsArray():array {
		$timeString = ( new DateTime() )->setTimestamp((int) ($this->rawTime / 1000))->format('Y-m-d H:i:s');
		$data = isset($this->data['trace']) ? $this->data["data"] : $this->data;

		return [
			'timestamp' => $timeString,
			'requestId' => $this->requestId,
			'type' => $this->type,
			'class' => $this->class,
			'scopeId' => $this->scopeId,
			'message' => $this->message,
			'data' => $data,
		];
	}

	public function __toString(): string {
		return json_encode($this->getAsArray());
	}
}