<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Product\Model;

use CpCreation\VitiCore\Behavior\HasImage;
use CpCreation\VitiCore\Behavior\HasTimestamp;
use CpCreation\VitiCore\Behavior\Impl\Image;
use CpCreation\VitiCore\Behavior\Impl\Timestamp;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

#[ORM\Entity]
#[ORM\Table(name: 'product_variant_image')]
#[ORM\HasLifecycleCallbacks]
class ProductVariantImage implements HasTimestamp, HasImage
{
    use Image;
    use Timestamp;

    #[ORM\Column(name: 'id', type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: ProductVariant::class, inversedBy: 'images')]
    #[ORM\JoinColumn(name: 'variant_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    #[Gedmo\SortableGroup]
    private ?ProductVariant $variant = null;

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

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

    public function getVariant(): ?ProductVariant
    {
        return $this->variant;
    }

    public function setVariant(?ProductVariant $variant): void
    {
        $this->variant = $variant;
    }

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

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

    #[Assert\Callback]
    public function validate(ExecutionContextInterface $context): void
    {
        if (!$this->getImage()) {
            $context->buildViolation('Veuillez ajouter une image')
                ->atPath('image.file')
                ->addViolation();
        }
    }
}
