<?php

namespace AppBundle\Entity\Product;

use AppBundle\Annotation\ExportSplitFields;
use Doctrine\ORM\Mapping as ORM;
use Dunglas\ApiBundle\Annotation\Iri;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use AppBundle\Entity\Traits\TimestampableEntity;
use AppBundle\Entity\Traits\CreatorUpdaterEntity;

/**
 * Equipment composé d'autres Equipments
 * Fais le lien entre un équipement parent et des équipements enfants
 *
 * @Iri("http://schema.org/EquipmentComposition")
 */
#[ORM\Entity]
class EquipmentComposition
{
    use TimestampableEntity;
    use CreatorUpdaterEntity;
    /**
     * @var int
     */
    #[ORM\Column(type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[Groups(['equipmentcomposition', 'equipment_list', 'equipment_details'])]
    private $id;
    /**
     * @var Equipment
     */
    #[ORM\ManyToOne(targetEntity: 'Equipment', inversedBy: 'equipmentComposition')]
    #[ORM\JoinColumn(nullable: false)]
    #[Assert\NotNull]
    #[Groups(['equipmentcomposition'])]
    private $parent;
    /**
     * @var Equipment
     */
    #[ORM\ManyToOne(targetEntity: 'Equipment')]
    #[ORM\JoinColumn(nullable: false)]
    #[Assert\NotNull]
    #[Groups(['equipmentcomposition', 'equipment_list_equipmentcomposition', 'equipment_details_equipmentcomposition'])]
    private $children;
    /**
     * @var int
     */
    #[ORM\Column(type: 'integer')]
    #[Assert\Type(type: 'integer', message: 'invalid-integer')]
    #[Assert\NotNull]
    #[Groups(['equipmentcomposition', 'equipment_details_equipmentcomposition'])]
    private $quantity;
    /**
     * @var string
     * @ExportSplitFields({"children.equipmentList.familly","children.brand","children.model"})
     */
    #[Groups(['template'])]
    private $fullLabelTemplate;
    /**
     * Sets id.
     *
     * @param int $id
     *
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }
    /**
     * Gets id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Sets parent.
     *
     * @param Equipment $parent
     *
     * @return $this
     */
    public function setParent(Equipment $parent)
    {
        $this->parent = $parent;

        return $this;
    }
    /**
     * Gets parent.
     *
     * @return Equipment
     */
    public function getParent()
    {
        return $this->parent;
    }
    /**
     * Sets children.
     *
     * @param Equipment $children
     *
     * @return $this
     */
    public function setChildren(Equipment $children)
    {
        $this->children = $children;

        return $this;
    }
    /**
     * Gets children.
     *
     * @return Equipment
     */
    public function getChildren()
    {
        return $this->children;
    }
    /**
     * Sets quantity.
     *
     * @param int $quantity
     *
     * @return $this
     */
    public function setQuantity($quantity)
    {
        $this->quantity = intval($quantity);

        return $this;
    }
    /**
     * Gets quantity.
     *
     * @return int
     */
    public function getQuantity()
    {
        return $this->quantity;
    }
    /**
     * Gets full label.
     *
     * @return array
     */
    public function getFullLabelTemplate()
    {
        $children = $this->getChildren();

        if(empty($children))
            return [];

       return [
           ['value' => $children->getEquipmentList() ? $children->getEquipmentList()->getFamilly() : '', 'translate' => true],
           $children->getBrand(),
           $children->getModel()
       ];
    }
}
