<?php

declare(strict_types=1);

namespace App\Entity;

use App\Enum\GameModeType;
use App\Repository\GameModeRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: GameModeRepository::class)]
#[ORM\UniqueConstraint(name: 'uniq_game_mode_key', columns: ['key_name'])]
#[ORM\HasLifecycleCallbacks]
class GameMode
{
    #[ORM\Id]
    #[ORM\Column(type: Types::GUID, unique: true)]
    private string $id;

    #[ORM\Column(name: 'key_name', length: 50, unique: true)]
    #[Assert\NotBlank]
    private string $key;

    #[ORM\Column(length: 50)]
    #[Assert\NotBlank]
    private string $name;

    #[ORM\Column(enumType: GameModeType::class, length: 10)]
    private GameModeType $type = GameModeType::X01;

    #[ORM\Column]
    #[Assert\Positive]
    private int $startScore;

    #[ORM\Column(type: Types::JSON)]
    private array $rules = [
        'bust' => 'TURN',
        'checkout' => 'EXACT',
        'doubleOut' => false,
    ];

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

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

    public function __construct(string $key, string $name, int $startScore)
    {
        $this->id = Uuid::v7()->toRfc4122();
        $this->key = $key;
        $this->name = $name;
        $this->startScore = $startScore;
        $this->createdAt = new \DateTimeImmutable();
        $this->updatedAt = new \DateTimeImmutable();
    }

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

    public function getKey(): string
    {
        return $this->key;
    }

    public function setKey(string $key): self
    {
        $this->key = $key;

        return $this;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

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

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

        return $this;
    }

    public function getStartScore(): int
    {
        return $this->startScore;
    }

    public function setStartScore(int $startScore): self
    {
        $this->startScore = $startScore;

        return $this;
    }

    public function getRules(): array
    {
        return $this->rules;
    }

    public function setRules(array $rules): self
    {
        $this->rules = $rules;

        return $this;
    }

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

    public function getUpdatedAt(): \DateTimeImmutable
    {
        return $this->updatedAt;
    }

    #[ORM\PrePersist]
    public function onPrePersist(): void
    {
        $now = new \DateTimeImmutable();
        $this->createdAt = $now;
        $this->updatedAt = $now;
    }

    #[ORM\PreUpdate]
    public function onPreUpdate(): void
    {
        $this->updatedAt = new \DateTimeImmutable();
    }
}
