<?php

declare(strict_types=1);

namespace App\Entity\Core;

use App\Entity\Place\PlaceControl;
use App\Entity\Place\RoomControl;
use App\Entity\Product\EquipmentControl;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Contrôle du bon état des Place, Room et Equipment, effectué ou planifié
 * Classe de base de @see PlaceControl, RoomControl, EquipmentControl.
 */
#[ORM\Entity]
#[ORM\Table(name: 'Control')]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
#[ORM\DiscriminatorMap(
    [
        'equipment' => EquipmentControl::class,
        'place' => PlaceControl::class,
        'room' => RoomControl::class,
    ]
)]
abstract class AbstractControl
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    protected ?int $id = null;

    /** @var Collection<int, Tagg> */
    #[ORM\ManyToMany(targetEntity: Tagg::class, inversedBy: 'controls')]
    #[ORM\JoinTable('tag_control')]
    #[ORM\JoinColumn(name: 'control_id')]
    #[ORM\InverseJoinColumn(name: 'tag_id')]
    protected Collection $tags;

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

    public function getTags(): Collection
    {
        return $this->tags;
    }

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

        return $this;
    }

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

        return $this;
    }
}
