<?php

/*
 * This file is part of the VitiCore 2.
 *
 * (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\Shop\Model;

use CpCreation\VitiCore\Behavior\HasDisable;
use CpCreation\VitiCore\Behavior\HasTranslatable;
use CpCreation\VitiCore\Behavior\Impl\Disable;
use CpCreation\VitiCore\Behavior\Impl\Translatable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

#[ORM\Entity(repositoryClass: 'CpCreation\VitiCore\Shop\Repository\DeliveryMethodCategoryRepository')]
#[ORM\Table(name: 'shop_delivery_method_category')]
#[ORM\HasLifecycleCallbacks]
class DeliveryMethodCategory implements HasTranslatable, HasDisable
{
    use Disable;
    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;

    #[ORM\Column(type: 'boolean')]
    private bool $minView;

    #[ORM\Column(type: 'boolean', options: ['default' => false])]
    private bool $disabledShippingAddress = false;

    #[ORM\OneToMany(targetEntity: DeliveryMethod::class, mappedBy: 'category', orphanRemoval: true)]
    #[ORM\JoinColumn(name: 'method_category_id', referencedColumnName: 'id', nullable: true)]
    #[ORM\OrderBy(['position' => 'ASC'])]
    private Collection $methods;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
        $this->disabled = false;
        $this->methods = new ArrayCollection();
        $this->minView = 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 isMinView(): bool
    {
        return $this->minView;
    }

    public function setMinView(bool $minView): void
    {
        $this->minView = $minView;
    }

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

    public function setDisabledShippingAddress(bool $disabledShippingAddress): void
    {
        $this->disabledShippingAddress = $disabledShippingAddress;
    }

    public function getMethods(): Collection
    {
        return $this->methods;
    }

    #[Assert\Callback]
    public function validate(ExecutionContextInterface $context): void
    {
        if ($this->isMinView()) {
            if (!$this->getMinViewTextClose() || !$this->getMinViewTextOpen()) {
                $message = 'This value should not be blank.';
                $context->buildViolation($message)
                    ->atPath("translations['fr'].minViewTextOpen")
                    ->addViolation();

                $context->buildViolation($message)
                    ->atPath("translations['fr'].minViewTextClose")
                    ->addViolation();
            }
        }
    }
}
