<?php

namespace App\Inventory\Entity;

use App\Inventory\Repository\InventoryPackageRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

/**
 * A named bundle of consumable items owned by a tenant (doctor/clinic). The
 * package price and its availability are derived from its component items at
 * read time — never stored — so they always reflect current item prices/stock.
 */
#[ORM\Entity(repositoryClass: InventoryPackageRepository::class)]
#[ORM\Table(name: 'inventory_packages')]
#[ORM\Index(columns: ['entity_type', 'entity_id'], name: 'idx_inventory_packages_owner')]
class InventoryPackage
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 36, unique: true)]
    private string $uuid;

    #[ORM\Column(name: 'entity_type', type: 'string', length: 10)]
    private string $entityType;

    #[ORM\Column(name: 'entity_id', type: 'integer')]
    private int $entityId;

    #[ORM\Column(type: 'string', length: 120)]
    private string $title;

    /** @var Collection<int, InventoryPackageItem> */
    #[ORM\OneToMany(mappedBy: 'package', targetEntity: InventoryPackageItem::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
    private Collection $items;

    #[ORM\Column(name: 'created_at', type: 'integer')]
    private int $createdAt;

    #[ORM\Column(name: 'updated_at', type: 'integer')]
    private int $updatedAt;

    public function __construct(string $entityType, int $entityId, string $title)
    {
        $this->uuid       = Uuid::v4()->toRfc4122();
        $this->entityType = $entityType;
        $this->entityId   = $entityId;
        $this->title      = $title;
        $this->items      = new ArrayCollection();
        $this->createdAt  = time();
        $this->updatedAt  = time();
    }

    public function getId(): ?int           { return $this->id; }
    public function getUuid(): string       { return $this->uuid; }
    public function getEntityType(): string { return $this->entityType; }
    public function getEntityId(): int      { return $this->entityId; }
    public function getTitle(): string      { return $this->title; }

    public function setTitle(string $v): self { $this->title = $v; $this->updatedAt = time(); return $this; }

    /** @return Collection<int, InventoryPackageItem> */
    public function getItems(): Collection { return $this->items; }

    public function addItem(InventoryPackageItem $item): self
    {
        if (!$this->items->contains($item)) {
            $this->items->add($item);
            $item->setPackage($this);
        }
        $this->updatedAt = time();
        return $this;
    }

    /** Drop every component item (used before re-populating on update). */
    public function clearItems(): self
    {
        $this->items->clear();
        $this->updatedAt = time();
        return $this;
    }
}
