<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Product\Model;

use CpCreation\VitiCore\Behavior\HasDisable;
use CpCreation\VitiCore\Behavior\HasTimestamp;
use CpCreation\VitiCore\Behavior\HasTranslatable;
use CpCreation\VitiCore\Behavior\Impl\Disable;
use CpCreation\VitiCore\Behavior\Impl\Timestamp;
use CpCreation\VitiCore\Behavior\Impl\Translatable;
use CpCreation\VitiCore\Product\Repository\ProductCollectionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Sortable\Sortable;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: ProductCollectionRepository::class)]
#[ORM\Table(name: 'product_collection')]
#[ORM\HasLifecycleCallbacks]
class ProductCollection implements HasTimestamp, Sortable, HasTranslatable, HasDisable
{
    use Disable;
    use Timestamp;
    use Translatable;

    #[ORM\Id]
    #[ORM\Column(type: 'uuid')]
    #[ORM\GeneratedValue(strategy: 'NONE')]
    private UuidInterface $id;

    #[ORM\Column(type: 'integer')]
    #[Gedmo\SortablePosition]
    private ?int $position = null;

    /**
     * @var Collection<int, ProductCollectionItem>
     */
    #[ORM\OneToMany(targetEntity: ProductCollectionItem::class, mappedBy: 'collection', cascade: ['persist', 'remove'], orphanRemoval: true)]
    #[ORM\OrderBy(['position' => 'ASC', 'id' => 'ASC'])]
    #[Assert\Valid]
    private Collection $items;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
        $this->items = new ArrayCollection();
        $this->disabled = false;
    }

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

    public function getPosition(): ?int
    {
        return $this->position;
    }

    public function setPosition(int $position): void
    {
        $this->position = $position;
    }

    public function addItem(ProductCollectionItem $item): self
    {
        if (!$this->items->contains($item)) {
            $this->items[] = $item;
            $item->setCollection($this);
        }

        return $this;
    }

    public function removeItem(ProductCollectionItem $item): self
    {
        if ($this->items->removeElement($item) && $item->getCollection() === $this) {
            $item->setCollection(null);
        }

        return $this;
    }

    /**
     * @return Collection<int, ProductCollectionItem>
     */
    public function getItems(): Collection
    {
        return $this->items;
    }

    /**
     * @return list<Product>
     */
    public function getProducts(): array
    {
        $products = [];

        foreach ($this->items as $item) {
            $product = $item->getProduct();
            if ($product instanceof Product) {
                $products[] = $product;
            }
        }

        return $products;
    }
}
