<?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\SlugTrait;
use AutoAdmin\Bundle\Content\Entity\Traits\TitleTrait;
use AutoAdmin\Bundle\Content\Entity\Traits\UpdatedAtTrait;
use AutoAdmin\Bundle\Content\Model\CreatedAtInterface;
use AutoAdmin\Bundle\Content\Model\IdInterface;
use AutoAdmin\Bundle\Content\Model\SiteEntityInterface;
use AutoAdmin\Bundle\Content\Model\SlugInterface;
use AutoAdmin\Bundle\Content\Model\TitleInterface;
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: 'site')]
#[ORM\Entity(repositoryClass: 'AutoAdmin\Bundle\Content\Repository\SiteRepository')]
class Site implements TitleInterface, SlugInterface, IdInterface
{
    use IdTrait;
    use CreatedAtTrait;
    use UpdatedAtTrait;
    use TitleTrait;
    use SlugTrait;

    #[ORM\OneToMany(mappedBy: 'site', targetEntity: Domain::class, cascade: ['ALL'], orphanRemoval: true)]
    protected Collection $domains;

    #[ORM\Column(type: Types::BOOLEAN, nullable: false, options: ['default' => true])]
    protected bool $enabled = true;

    #[ORM\OneToOne(mappedBy: 'site', targetEntity: Logo::class, cascade: ['ALL'])]
    protected ?Logo $logo = null;

    public function __construct(string $slug = null, string $name = null, $logo = null)
    {
        if ($slug) {
            $this->slug = $slug;
        }

        if ($name) {
            $this->title = $name;
        }

        if ($logo) {
            $this->logo = $logo;
        }

        $this->domains = new ArrayCollection();
        $this->enabled = true;
    }

    public function getDomains(): Collection
    {
        if (empty($this->domains)) {
            return new ArrayCollection();
        }

        return $this->domains;
    }

    public function setDomains(Collection $domains): Site
    {
        $this->domains = $domains;

        return $this;
    }

    public function addDomain(Domain $domain): void
    {
        if (!$this->domains->contains($domain)) {
            $domain->setSite($this);
            $this->domains->add($domain);
        }
    }

    public function removeDomain(Domain $domain): void
    {
        if ($this->domains->contains($domain)) {
            $domain->setSite(null);
            $this->domains->removeElement($domain);
        }
    }

    public function getEnabled(): bool
    {
        return $this->enabled;
    }

    public function setEnabled(bool $enabled): self
    {
        $this->enabled = $enabled;

        return $this;
    }

    public function isSameAs(Site $site): bool
    {
        return $this->getId() == $site->getId();
    }

    public function getLogo(): ?Logo
    {
        return $this->logo;
    }

    public function setLogo($logo): self
    {
        $this->logo = $logo;

        return $this;
    }
}
