<?php

namespace App\Inventory\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * A line in an {@see InventoryPackage}: a reference to an {@see InventoryItem}
 * plus the quantity of it the package contains.
 */
#[ORM\Entity]
#[ORM\Table(name: 'inventory_package_items')]
class InventoryPackageItem
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: InventoryPackage::class, inversedBy: 'items')]
    #[ORM\JoinColumn(name: 'package_id', nullable: false, onDelete: 'CASCADE')]
    private InventoryPackage $package;

    #[ORM\ManyToOne(targetEntity: InventoryItem::class)]
    #[ORM\JoinColumn(name: 'item_id', nullable: false, onDelete: 'CASCADE')]
    private InventoryItem $item;

    #[ORM\Column(type: 'integer')]
    private int $amount = 1;

    public function __construct(InventoryItem $item, int $amount)
    {
        $this->item   = $item;
        $this->amount = max(1, $amount);
    }

    public function getId(): ?int              { return $this->id; }
    public function getPackage(): InventoryPackage { return $this->package; }
    public function getItem(): InventoryItem   { return $this->item; }
    public function getAmount(): int           { return $this->amount; }

    public function setPackage(InventoryPackage $p): self { $this->package = $p; return $this; }
    public function setAmount(int $v): self               { $this->amount = max(1, $v); return $this; }
}
