<?php

declare(strict_types=1);

namespace App\Entity\Tenant;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'widget_zones')]
#[ORM\HasLifecycleCallbacks]
class WidgetZone
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: Types::INTEGER)]
    private ?int $id = null;

    #[ORM\Column(type: Types::STRING, length: 100)]
    private string $name;

    #[ORM\Column(type: Types::STRING, length: 100, unique: true)]
    private string $slug;

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

    #[ORM\Column(type: Types::STRING, length: 20)]
    private string $zoneType;

    #[ORM\Column(type: Types::BOOLEAN, options: ['default' => true])]
    private bool $isActive = true;

    /** @var Collection<int, WidgetPlacement> */
    #[ORM\OneToMany(targetEntity: WidgetPlacement::class, mappedBy: 'zone', cascade: ['persist', 'remove'], orphanRemoval: true)]
    #[ORM\OrderBy(['position' => 'ASC'])]
    private Collection $placements;

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
    private \DateTimeImmutable $createdAt;

    private const VALID_ZONE_TYPES = ['homepage', 'sidebar', 'footer', 'header', 'detail', 'category'];

    public function __construct()
    {
        $this->placements = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getSlug(): string
    {
        return $this->slug;
    }

    public function setSlug(string $slug): self
    {
        $this->slug = $slug;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getZoneType(): string
    {
        return $this->zoneType;
    }

    public function setZoneType(string $zoneType): self
    {
        if (!in_array($zoneType, self::VALID_ZONE_TYPES, true)) {
            throw new \InvalidArgumentException(
                sprintf('Invalid zone type "%s". Allowed: %s', $zoneType, implode(', ', self::VALID_ZONE_TYPES))
            );
        }

        $this->zoneType = $zoneType;

        return $this;
    }

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

    public function setIsActive(bool $isActive): self
    {
        $this->isActive = $isActive;

        return $this;
    }

    /** @return Collection<int, WidgetPlacement> */
    public function getPlacements(): Collection
    {
        return $this->placements;
    }

    public function addPlacement(WidgetPlacement $placement): self
    {
        if (!$this->placements->contains($placement)) {
            $this->placements->add($placement);
            $placement->setZone($this);
        }

        return $this;
    }

    public function removePlacement(WidgetPlacement $placement): self
    {
        if ($this->placements->removeElement($placement)) {
            if ($placement->getZone() === $this) {
                $placement->setZone($this);
            }
        }

        return $this;
    }

    public function getCreatedAt(): \DateTimeImmutable
    {
        return $this->createdAt;
    }

    #[ORM\PrePersist]
    public function onPrePersist(): void
    {
        $this->createdAt = new \DateTimeImmutable();
    }
}
