<?php


namespace AutoAdmin\Bundle\Content\Entity;


use AutoAdmin\Bundle\Content\Entity\Traits\CreatedAtTrait;
use AutoAdmin\Bundle\Content\Entity\Traits\IdTrait;
use AutoAdmin\Bundle\Content\Entity\Traits\UpdatedAtTrait;
use AutoAdmin\Bundle\Content\Model\CreatedAtInterface;
use AutoAdmin\Bundle\Content\Model\DomainEntityInterface;
use AutoAdmin\Bundle\Content\Model\LocaleInterface;
use AutoAdmin\Bundle\Content\Model\UpdatedAtInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Table(name: 'domain')]
#[ORM\Entity(repositoryClass: 'AutoAdmin\Bundle\Content\Repository\DomainRepository')]
class Domain implements CreatedAtInterface, UpdatedAtInterface, DomainEntityInterface
{
    use IdTrait;
    use CreatedAtTrait;
    use UpdatedAtTrait;

    #[ORM\Column(name: 'domain', type: Types::STRING, length: 255)]
    protected string $domain;

    /**
     * @var Collection<LocaleInterface>
     */
    #[ORM\ManyToMany(targetEntity: Locale::class)]
    protected Collection $locales;

    #[ORM\ManyToOne(targetEntity: Site::class, inversedBy: 'domains')]
    protected ?Site $site = null;

    public function __construct(string $domain = null)
    {
        if ($domain) {
            $this->domain = $domain;
        }

        $this->locales = new ArrayCollection();
    }

    public function __toString(): string
    {
        return $this->domain . '#' . $this->getId();
    }

    public function getDomain(): string
    {
        return $this->domain;
    }

    public function setDomain(?string $domain): self
    {
        $this->domain = $domain;

        return $this;
    }

    public function getLocales(): Collection
    {
        return $this->locales;
    }

    public function setLocales(Collection $locales): self
    {
        $this->locales = $locales;

        return $this;
    }

    public function addLocale(Locale $locale): self
    {
        if (!$this->locales->contains($locale)) {
            $this->locales->add($locale);
        }

        return $this;
    }

    public function removeLocale(Locale $locale): self
    {
        if ($this->locales->contains($locale)) {
            $this->locales->removeElement($locale);
        }

        return $this;
    }

    public function getSite(): ?Site
    {
        return $this->site;
    }

    public function setSite(?Site $site): self
    {
        $this->site = $site;

        return $this;
    }
}
