<?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\IdInterface;
use AutoAdmin\Bundle\Content\Model\SlugInterface;
use AutoAdmin\Bundle\Content\Model\TitleInterface;
use AutoAdmin\Bundle\Media\Attributes\LinkedMedia;
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, options: ['default' => true])]
    protected bool $enabled = true;

//    #[ORM\Column(type: Types::STRING, nullable: true)]
//    #[LinkedMedia(mappings: ['image'])]
//    protected ?string $logo = null;

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

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

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

    public function getDomains(): Collection
    {
        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(): ?string
//    {
//        return $this->logo;
//    }
//
//    public function setLogo(?string $logo): Site
//    {
//        $this->logo = $logo;
//
//        return $this;
//    }
}
