<?php

namespace App\Entity;

use App\Repository\ImageRepository;
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: ImageRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_NAME', fields: ['name'])]
#[UniqueEntity(fields: ['name'], message: 'validators.image.name.unique')]
class Image extends AbstractEntity
{
    use NameableTrait;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: true)]
    private ?SoftwareProfile $softwareProfile = null;

    #[ORM\Column]
    private ?bool $remotePc = null;

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

    #[ORM\ManyToOne(targetEntity: self::class)]
    private ?self $parent = null;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn( onDelete: 'SET NULL')]
    private ?Client $client = null;

    #[ORM\Column]
    private ?bool $isGlobal = null;

    /**
     * @var Collection<int, ImageImageRepository>
     */
    #[ORM\OneToMany(mappedBy: 'image', targetEntity: ImageImageRepository::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $imageImageRepositories;

    #[ORM\Column(nullable: true)]
    private ?int $version = null;

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

    public function __construct()
    {
        parent::__construct();
        $this->imageImageRepositories = new ArrayCollection();
    }

    public function getSoftwareProfile(): ?SoftwareProfile
    {
        return $this->softwareProfile;
    }

    public function setSoftwareProfile(?SoftwareProfile $softwareProfile): static
    {
        $this->softwareProfile = $softwareProfile;

        return $this;
    }

    public function isRemotePc(): ?bool
    {
        return $this->remotePc;
    }

    public function setRemotePc(bool $remotePc): static
    {
        $this->remotePc = $remotePc;

        return $this;
    }

    public function getPartitionInfo(): ?string
    {
        return $this->partitionInfo;
    }

    public function setPartitionInfo(?string $partitionInfo): static
    {
        $this->partitionInfo = $partitionInfo;

        return $this;
    }

    public function getParent(): ?self
    {
        return $this->parent;
    }

    public function setParent(?self $parent): static
    {
        $this->parent = $parent;

        return $this;
    }

    public function getClient(): ?Client
    {
        return $this->client;
    }

    public function setClient(?Client $client): static
    {
        $this->client = $client;

        return $this;
    }

    public function isGlobal(): ?bool
    {
        return $this->isGlobal;
    }

    public function setIsGlobal(bool $isGlobal): static
    {
        $this->isGlobal = $isGlobal;

        return $this;
    }

    /**
     * @return Collection<int, ImageImageRepository>
     */
    public function getImageImageRepositories(): Collection
    {
        return $this->imageImageRepositories;
    }

    public function addImageImageRepository(ImageImageRepository $imageImageRepository): static
    {
        if (!$this->imageImageRepositories->contains($imageImageRepository)) {
            $this->imageImageRepositories->add($imageImageRepository);
            $imageImageRepository->setImage($this);
        }

        return $this;
    }

    public function removeImageImageRepository(ImageImageRepository $imageImageRepository): static
    {
        if ($this->imageImageRepositories->removeElement($imageImageRepository)) {
            // set the owning side to null (unless already changed)
            if ($imageImageRepository->getImage() === $this) {
                $imageImageRepository->setImage(null);
            }
        }

        return $this;
    }

    public function containsImageImageRepository(ImageImageRepository $imageImageRepository): bool
    {
        foreach ($this->imageImageRepositories as $existing) {
            if (
                $existing->getImage()->getId() === $imageImageRepository->getImage()->getId() &&
                $existing->getRepository()->getId() === $imageImageRepository->getRepository()->getId()
            ) {
                return true;
            }
        }

        return false;
    }

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

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

        return $this;
    }

    public function getType(): ?string
    {
        return $this->type;
    }

    public function setType(string $type): static
    {
        $this->type = $type;

        return $this;
    }
}
