<?php

namespace App\Entity;

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

#[ORM\Entity(repositoryClass: PxeTemplateRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_NAME', fields: ['name'])]
#[UniqueEntity(fields: ['name'], message: 'validators.pxe_template.name.unique')]
class PxeTemplate extends AbstractEntity
{
    use NameableTrait;
    use SynchronizedTrait;

    #[ORM\Column(type: Types::TEXT)]
    private ?string $templateContent = null;

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

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

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

    public function getTemplateContent(): ?string
    {
        return $this->templateContent;
    }

    public function setTemplateContent(string $templateContent): static
    {
        $this->templateContent = $templateContent;

        return $this;
    }

    /**
     * @return Collection<int, Client>
     */
    public function getClients(): Collection
    {
        return $this->clients;
    }

    public function setClients(array $clients): static
    {
        $this->clients->clear();

        foreach ($clients as $client){
            $this->addClient($client);
        }

        return $this;
    }

    public function addClient(Client $client): static
    {
        if (!$this->clients->contains($client)) {
            $this->clients->add($client);
        }

        return $this;
    }

    public function removeClient(Client $client): static
    {
        if ($this->clients->removeElement($client)) {
            if ($client->getTemplate() === $this) {
                $client->setTemplate(null);
            }
        }

        return $this;
    }

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

    public function setDefault(?bool $isDefault): static
    {
        $this->isDefault = $isDefault;

        return $this;
    }
}
