<?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\Impl\Disable;
use CpCreation\VitiCore\Product\Model\ProductPriceGroup;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

#[ORM\Entity(repositoryClass: 'CpCreation\VitiCore\Shop\Repository\ShippingCountryRepository')]
#[ORM\Table(name: 'shop_shipping_country')]
#[ORM\HasLifecycleCallbacks]
class ShippingCountry implements HasDisable
{
    use Disable;

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

    #[ORM\Column(type: 'string')]
    #[Assert\NotBlank]
    private ?string $code = null;

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

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

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

    #[ORM\ManyToMany(targetEntity: ShippingZone::class, mappedBy: 'countries')]
    private Collection $zones;

    #[ORM\ManyToMany(targetEntity: ProductPriceGroup::class)]
    #[Assert\Count(min: 1, minMessage: 'Vous devez ajouter au moins un groupe')]
    private Collection $groups;

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

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

    public function getCode(): ?string
    {
        return $this->code;
    }

    public function setCode(string $code): void
    {
        $this->code = $code;
    }

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

    public function setCounty(bool $county): void
    {
        $this->county = $county;
    }

    public function getMinQty(): ?int
    {
        return $this->minQty;
    }

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

    public function getMultiple(): ?int
    {
        return $this->multiple;
    }

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

    public function getZones(): Collection
    {
        return $this->zones;
    }

    public function addZone(ShippingZone $zone): self
    {
        if (!$this->zones->contains($zone)) {
            $this->zones[] = $zone;
        }

        return $this;
    }

    public function removeZone(ShippingZone $zone): void
    {
        if ($this->zones->contains($zone)) {
            $this->zones->removeElement($zone);
        }
    }

    public function addGroup(ProductPriceGroup $group): self
    {
        $this->groups[] = $group;

        return $this;
    }

    public function removeGroup(ProductPriceGroup $group): void
    {
        $this->groups->removeElement($group);
    }

    public function getGroups(): Collection
    {
        return $this->groups;
    }

    public function getName(): ?string
    {
        if ($this->getCode()) {
            return Countries::getName($this->getCode());
        }

        return null;
    }

    #[Assert\Callback]
    public function validate(ExecutionContextInterface $context): void
    {
        if (null !== $this->minQty && null === $this->multiple) {
            $context->buildViolation('Le multiple doit être renseigné lorsque la quantité minimum est définie.')
                ->atPath('multiple')
                ->addViolation();
        }

        if (null !== $this->multiple && null === $this->minQty) {
            $context->buildViolation('La quantité minimum doit être renseignée lorsque le multiple est défini.')
                ->atPath('minQty')
                ->addViolation();
        }
    }
}
