<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\DrinkTypeRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: DrinkTypeRepository::class)]
#[ORM\Table(name: 'drink_type')]
class DrinkType
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private null|int $id = null;

    #[ORM\Column(type: 'datetime_immutable')]
    private readonly DateTimeImmutable $createdAt;

    #[ORM\Column(type: 'datetime_immutable')]
    private DateTimeImmutable $updatedAt;

    #[ORM\OneToMany(mappedBy: 'drinkType', targetEntity: InventoryRecord::class)]
    private Collection $inventoryRecords;

    #[ORM\OneToMany(mappedBy: 'drinkType', targetEntity: OrderItem::class)]
    private Collection $orderItems;

    public function __construct(
        #[ORM\Column(type: 'string', length: 255, unique: true)]
        private string $name,
        #[ORM\Column(type: 'text', nullable: true)]
        private null|string $description = null,
        #[ORM\Column(type: 'integer')]
        private int $desiredStock = 10,
        null|DateTimeImmutable $createdAt = null,
        null|DateTimeImmutable $updatedAt = null,
    ) {
        $this->createdAt = $createdAt ?? new DateTimeImmutable();
        $this->updatedAt = $updatedAt ?? new DateTimeImmutable();
        $this->inventoryRecords = new ArrayCollection();
        $this->orderItems = new ArrayCollection();
    }

    public function getId(): null|int
    {
        return $this->id;
    }

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

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

    public function getDescription(): null|string
    {
        return $this->description;
    }

    public function setDescription(null|string $description): self
    {
        $this->description = $description;
        $this->updateTimestamp();
        return $this;
    }

    public function getDesiredStock(): int
    {
        return $this->desiredStock;
    }

    public function setDesiredStock(int $desiredStock): self
    {
        $this->desiredStock = $desiredStock;
        $this->updateTimestamp();
        return $this;
    }

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

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

    public function getInventoryRecords(): Collection
    {
        return $this->inventoryRecords;
    }

    public function getOrderItems(): Collection
    {
        return $this->orderItems;
    }

    private function updateTimestamp(): void
    {
        $this->updatedAt = new DateTimeImmutable();
    }
}
