<?php

declare(strict_types=1);

namespace App\Entity\Core;

use App\Entity\Place\PlaceRepair;
use App\Entity\Place\RoomRepair;
use App\Entity\Product\EquipmentRepair;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Classe de base de @see PlaceRepair, RoomRepair, EquipmentRepair.
 */
#[ORM\Entity]
#[ORM\Table(name: 'Repair')]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
#[ORM\DiscriminatorMap(
    [
        'room' => RoomRepair::class,
        'place' => PlaceRepair::class,
        'equipment' => EquipmentRepair::class,
    ]
)]
abstract class AbstractRepair
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    protected ?int $id = null;

    /** @var Collection<int, Tagg> */
    #[ORM\ManyToMany(targetEntity: Tagg::class, inversedBy: 'repairs', cascade: ['persist'], orphanRemoval: false)]
    #[ORM\JoinTable(name: 'tag_repair')]
    #[ORM\JoinColumn(name: 'repair_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->addRepair($this);
        }

        return $this;
    }

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

        return $this;
    }
}
