<?php

namespace App\Entity;

use App\Repository\ModRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

#[ORM\Entity(repositoryClass: ModRepository::class)]
#[ORM\UniqueConstraint(name: 'mod_unique', columns: ['nom', 'version'])]
#[UniqueEntity(fields: ['nom', 'version'], message: 'Mod already exists')]
class Mod
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 50)]
    private ?string $nom = null;

    #[ORM\Column(length: 50)]
    private ?string $version = null;

    #[ORM\Column(length: 255,unique: true)]
    private ?string $uri = null;

    /**
     * @var Collection<int, self>
     */
    #[ORM\ManyToMany(targetEntity: self::class, inversedBy: 'soumis')]
    private Collection $dependances;

    /**
     * @var Collection<int, self>
     *     .
     */
    #[ORM\ManyToMany(targetEntity: self::class, mappedBy: 'dependances')]
    private Collection $soumis;

    /**
     * @var Collection<int, Constitue>
     */
    #[ORM\OneToMany(targetEntity: Constitue::class, mappedBy: 'mod')]
    private Collection $constitues;


    public function __construct()
    {
        $this->soumis = new ArrayCollection();
        $this->dependances = new ArrayCollection();
        $this->constitues = new ArrayCollection();
    }

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

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(string $nom): static
    {
        $this->nom = $nom;

        return $this;
    }

    public function getVersion(): ?string
    {
        return $this->version;
    }

    public function setVersion(string $version): static
    {
        $this->version = $version;

        return $this;
    }

    public function getUri(): ?string
    {
        return $this->uri;
    }

    public function setUri(string $uri): static
    {
        $this->uri = $uri;

        return $this;
    }

    /**
     * @return Collection<int, self>
     */
    public function getSoumis(): Collection
    {
        return $this->soumis;
    }

    public function addSoumis(self $soumi): static
    {
        if (!$this->soumis->contains($soumi)) {
            $this->soumis->add($soumi);
        }

        return $this;
    }

    public function removeSoumis(self $soumi): static
    {
        $this->soumis->removeElement($soumi);

        return $this;
    }

    /**
     * @return Collection<int, self>
     */
    public function getDependances(): Collection
    {
        return $this->dependances;
    }

    public function addDependance(self $dependance): static
    {
        if (!$this->dependances->contains($dependance)) {
            $this->dependances->add($dependance);
            $dependance->addSoumis($this);
        }

        return $this;
    }

    public function removeDependance(self $dependance): static
    {
        if ($this->dependances->removeElement($dependance)) {
            $dependance->removeSoumis($this);
        }

        return $this;
    }

    /**
     * @return Collection<int, Constitue>
     */
    public function getConstitues(): Collection
    {
        return $this->constitues;
    }

    public function addConstitue(Constitue $constitue): static
    {
        if (!$this->constitues->contains($constitue)) {
            $this->constitues->add($constitue);
            $constitue->setMod($this);
        }

        return $this;
    }

    public function removeConstitue(Constitue $constitue): static
    {
        if ($this->constitues->removeElement($constitue)) {
            // set the owning side to null (unless already changed)
            if ($constitue->getMod() === $this) {
                $constitue->setMod(null);
            }
        }

        return $this;
    }

    public function isUsed(): bool{
        return !$this->constitues->isEmpty();
    }

    public function isADependance(): bool{
        return !$this->soumis->isEmpty();
    }

}
