<?php
declare (strict_types=1);

namespace App\Entity\Product;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Access\Access;
use App\Entity\Core\Tagg;
use App\Entity\Organization\Organization;
use App\Entity\Place\Place;
use App\Entity\Place\Room;

//use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @todo : A la suite de la migration, il faut supprimer le nom de la table pour avoir une table Equipment, et supprimer l'attribut discr.
 * @todo : migration table tag_product
 * Classe ... qui ...
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ADMIN\') and object.getOrganization().getId() == user.getOrganization().getId()'
        )
    ]
)]
//#[Auditable]
#[ORM\Entity]
#[ORM\Table(name: 'Product')]
class Equipment extends AbstractProduct
{
    #[ORM\Column(length: 255, nullable: false)]
    private string $discr = 'equipment';

    #[ORM\ManyToOne(inversedBy: 'equipments')]
    protected Organization $organization;

    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'equipments')]
    private Access $access;

    #[ORM\ManyToOne(inversedBy: 'equipmentStorages')]
    #[ORM\JoinColumn(name: 'place_storage_id', nullable: true, onDelete: 'SET NULL')]
    private Place $placeStorage;

    #[ORM\ManyToOne(inversedBy: 'equipmentStorages')]
    #[ORM\JoinColumn(name: 'room_storage_id', nullable: true, onDelete: 'SET NULL')]
    private Room $roomStorage;

    #[ORM\ManyToOne(inversedBy: 'equipmentUseds')]
    #[ORM\JoinColumn(name: 'place_used_id', nullable: true, onDelete: 'SET NULL')]
    private Place $placeWhereIsUsed;

    #[ORM\ManyToOne(inversedBy: 'equipmentUseds')]
    #[ORM\JoinColumn(name: 'room_used_id', nullable: true, onDelete: 'SET NULL')]
    private Room $roomWhereIsUsed;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: true)]
    private EquipmentList $equipmentList;

    #[ORM\OneToMany(mappedBy: 'parent', targetEntity: EquipmentComposition::class, orphanRemoval: true)]
    private Collection $equipmentComposition;

    #[ORM\OneToMany(mappedBy: 'equipment', targetEntity: EquipmentRepair::class, orphanRemoval: true)]
    private Collection $equipmentRepair;

    #[ORM\OneToMany(mappedBy: 'equipment', targetEntity: EquipmentControl::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $equipmentControl;

    #[ORM\OneToMany(mappedBy: 'equipment', targetEntity: EquipmentLoan::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $equipmentLoan;

    #[ORM\ManyToOne(inversedBy: 'equipmentSuppliers')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private Access $supplier;

    #[ORM\ManyToOne(inversedBy: 'equipmentControlManagers')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private Access $controlManager;

    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'equipmentEditors')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private Access $editor;

    #[ORM\ManyToMany(targetEntity: EquipmentList::class)]
    #[ORM\JoinColumn(name: 'equipment_id', referencedColumnName: 'id')]
    #[ORM\InverseJoinColumn(name: 'equipmentlist_id', referencedColumnName: 'id')]
    private Collection $instrumentOrVoice;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private Access $managerControl;

    #[ORM\ManyToMany(targetEntity: Tagg::class, inversedBy: 'equipments', cascade: ['persist'])]
    #[ORM\JoinTable(name: 'tag_product')]
    #[ORM\JoinColumn(name: 'product_id', referencedColumnName: 'id')]
    #[ORM\InverseJoinColumn(name: 'tag_id', referencedColumnName: 'id')]
    private Collection $tags;

    public function __construct()
    {
        $this->equipmentComposition = new ArrayCollection();
        $this->equipmentRepair = new ArrayCollection();
        $this->equipmentControl = new ArrayCollection();
        $this->equipmentLoan = new ArrayCollection();
        $this->instrumentOrVoice = new ArrayCollection();
        $this->tags = new ArrayCollection();
    }

    public function getDiscr(): ?string
    {
        return $this->discr;
    }

    public function setDiscr(string $discr): self
    {
        $this->discr = $discr;
        return $this;
    }

    public function getOrganization(): ?Organization
    {
        return $this->organization;
    }

    public function setOrganization(?Organization $organization): self
    {
        $this->organization = $organization;
        return $this;
    }

    public function getAccess(): ?Access
    {
        return $this->access;
    }

    public function setAccess(?Access $access): self
    {
        $this->access = $access;
        return $this;
    }

    public function getPlaceStorage(): ?Place
    {
        return $this->placeStorage;
    }

    public function setPlaceStorage(?Place $placeStorage): self
    {
        $this->placeStorage = $placeStorage;
        return $this;
    }

    public function getRoomStorage(): ?Room
    {
        return $this->roomStorage;
    }

    public function setRoomStorage(?Room $roomStorage): self
    {
        $this->roomStorage = $roomStorage;
        return $this;
    }

    public function getPlaceWhereIsUsed(): ?Place
    {
        return $this->placeWhereIsUsed;
    }

    public function setPlaceWhereIsUsed(?Place $placeWhereIsUsed): self
    {
        $this->placeWhereIsUsed = $placeWhereIsUsed;
        return $this;
    }

    public function getRoomWhereIsUsed(): ?Room
    {
        return $this->roomWhereIsUsed;
    }

    public function setRoomWhereIsUsed(?Room $roomWhereIsUsed): self
    {
        $this->roomWhereIsUsed = $roomWhereIsUsed;
        return $this;
    }

    public function getEquipmentList(): ?EquipmentList
    {
        return $this->equipmentList;
    }

    public function setEquipmentList(?EquipmentList $equipmentList): self
    {
        $this->equipmentList = $equipmentList;
        return $this;
    }

    /**
     * @return Collection<int, EquipmentComposition>
     */
    public function getEquipmentComposition(): Collection
    {
        return $this->equipmentComposition;
    }

    public function addEquipmentComposition(EquipmentComposition $equipmentComposition): self
    {
        if (!$this->equipmentComposition->contains($equipmentComposition)) {
            $this->equipmentComposition[] = $equipmentComposition;
            $equipmentComposition->setParent($this);
        }
        return $this;
    }

    public function removeEquipmentComposition(EquipmentComposition $equipmentComposition): self
    {
        if ($this->equipmentComposition->removeElement($equipmentComposition)) {
            // set the owning side to null (unless already changed)
            if ($equipmentComposition->getParent() === $this) {
                $equipmentComposition->setParent(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, EquipmentRepair>
     */
    public function getEquipmentRepair(): Collection
    {
        return $this->equipmentRepair;
    }

    public function addEquipmentRepair(EquipmentRepair $equipmentRepair): self
    {
        if (!$this->equipmentRepair->contains($equipmentRepair)) {
            $this->equipmentRepair[] = $equipmentRepair;
            $equipmentRepair->setEquipment($this);
        }
        return $this;
    }

    public function removeEquipmentRepair(EquipmentRepair $equipmentRepair): self
    {
        if ($this->equipmentRepair->removeElement($equipmentRepair)) {
            // set the owning side to null (unless already changed)
            if ($equipmentRepair->getEquipment() === $this) {
                $equipmentRepair->setEquipment(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, EquipmentControl>
     */
    public function getEquipmentControl(): Collection
    {
        return $this->equipmentControl;
    }

    public function addEquipmentControl(EquipmentControl $equipmentControl): self
    {
        if (!$this->equipmentControl->contains($equipmentControl)) {
            $this->equipmentControl[] = $equipmentControl;
            $equipmentControl->setEquipment($this);
        }
        return $this;
    }

    public function removeEquipmentControl(EquipmentControl $equipmentControl): self
    {
        if ($this->equipmentControl->removeElement($equipmentControl)) {
            // set the owning side to null (unless already changed)
            if ($equipmentControl->getEquipment() === $this) {
                $equipmentControl->setEquipment(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, EquipmentLoan>
     */
    public function getEquipmentLoan(): Collection
    {
        return $this->equipmentLoan;
    }

    public function addEquipmentLoan(EquipmentLoan $equipmentLoan): self
    {
        if (!$this->equipmentLoan->contains($equipmentLoan)) {
            $this->equipmentLoan[] = $equipmentLoan;
            $equipmentLoan->setEquipment($this);
        }
        return $this;
    }

    public function removeEquipmentLoan(EquipmentLoan $equipmentLoan): self
    {
        if ($this->equipmentLoan->removeElement($equipmentLoan)) {
            // set the owning side to null (unless already changed)
            if ($equipmentLoan->getEquipment() === $this) {
                $equipmentLoan->setEquipment(null);
            }
        }
        return $this;
    }

    public function getSupplier(): ?Access
    {
        return $this->supplier;
    }

    public function setSupplier(?Access $supplier): self
    {
        $this->supplier = $supplier;
        return $this;
    }

    public function getControlManager(): ?Access
    {
        return $this->controlManager;
    }

    public function setControlManager(?Access $controlManager): self
    {
        $this->controlManager = $controlManager;
        return $this;
    }

    public function getEditor(): ?Access
    {
        return $this->editor;
    }

    public function setEditor(?Access $editor): self
    {
        $this->editor = $editor;
        return $this;
    }

    /**
     * @return Collection<int, EquipmentList>
     */
    public function getInstrumentOrVoice(): Collection
    {
        return $this->instrumentOrVoice;
    }

    public function addInstrumentOrVoice(EquipmentList $instrumentOrVoice): self
    {
        if (!$this->instrumentOrVoice->contains($instrumentOrVoice)) {
            $this->instrumentOrVoice[] = $instrumentOrVoice;
        }
        return $this;
    }

    public function removeInstrumentOrVoice(EquipmentList $instrumentOrVoice): self
    {
        $this->instrumentOrVoice->removeElement($instrumentOrVoice);
        return $this;
    }

    public function getManagerControl(): ?Access
    {
        return $this->managerControl;
    }

    public function setManagerControl(?Access $managerControl): self
    {
        $this->managerControl = $managerControl;
        return $this;
    }

    /**
     * @return Collection<int, Tagg>
     */
    public function getTags(): Collection
    {
        return $this->tags;
    }

    public function addTag(Tagg $tag): self
    {
        if (!$this->tags->contains($tag)) {
            $this->tags[] = $tag;
        }
        return $this;
    }

    public function removeTag(Tagg $tag): self
    {
        $this->tags->removeElement($tag);
        return $this;
    }
}