<?php

/*
 * This file is part of the Serenity 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\Behavior\Impl;

use CpCreation\VitiCore\Media\Model\Image;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\HasLifecycleCallbacks]
trait Images
{
    #[ORM\ManyToMany(
        targetEntity: Image::class,
        cascade: ['persist', 'remove']
    )]
    private Collection $images;

    public function addImage(Image $image): void
    {
        if (!$this->images->contains($image)) {
            $this->images->add($image);
        }
    }

    public function removeImage(Image $image): void
    {
        $this->images->removeElement($image);
    }

    /**
     * @return Collection<int, Image>
     */
    public function getImages(): Collection
    {
        return $this->images;
    }

    #[ORM\PrePersist]
    public function generateImageCollectionIfEmpty(): void
    {
        if (!isset($this->images)) {
            $this->images = new ArrayCollection();
        }
    }
}
