<?php

declare(strict_types=1);

namespace App\Entity;

use App\Enum\GameEventType;
use App\Repository\GameEventLogRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: GameEventLogRepository::class)]
#[ORM\Index(name: 'idx_event_log_game_created_at', columns: ['game_id', 'created_at'])]
class GameEventLog
{
    #[ORM\Id]
    #[ORM\Column(type: Types::GUID, unique: true)]
    private string $id;

    #[ORM\ManyToOne(inversedBy: 'eventLogs')]
    #[ORM\JoinColumn(nullable: false)]
    private Game $game;

    #[ORM\Column(enumType: GameEventType::class, length: 40)]
    private GameEventType $type;

    #[ORM\Column(type: Types::JSON)]
    private array $payload = [];

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
    private \DateTimeImmutable $createdAt;

    public function __construct(Game $game, GameEventType $type, array $payload = [])
    {
        $this->id = Uuid::v7()->toRfc4122();
        $this->game = $game;
        $this->type = $type;
        $this->payload = $payload;
        $this->createdAt = new \DateTimeImmutable();
    }

    public function getId(): string
    {
        return $this->id;
    }

    public function getGame(): Game
    {
        return $this->game;
    }

    public function setGame(Game $game): self
    {
        $this->game = $game;

        return $this;
    }

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

    public function setType(GameEventType $type): self
    {
        $this->type = $type;

        return $this;
    }

    public function getPayload(): array
    {
        return $this->payload;
    }

    public function setPayload(array $payload): self
    {
        $this->payload = $payload;

        return $this;
    }

    public function getCreatedAt(): \DateTimeImmutable
    {
        return $this->createdAt;
    }
}
