<?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 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\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: 'CpCreation\VitiCore\Shop\Repository\ShippingZoneRepository')]
#[ORM\Table(name: 'shop_shipping_zone')]
#[ORM\HasLifecycleCallbacks]
class ShippingZone 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 $name = null;

    #[ORM\ManyToMany(targetEntity: ShippingCountry::class, inversedBy: 'zones')]
    #[ORM\JoinTable(name: 'shop_shipping_countries_zones')]
    #[ORM\OrderBy(['code' => 'ASC'])]
    #[Assert\Valid]
    private Collection $countries;

    #[ORM\OneToMany(targetEntity: ShippingCounty::class, mappedBy: 'zone', cascade: ['persist'])]
    #[ORM\OrderBy(['code' => 'ASC'])]
    #[Assert\Valid]
    private Collection $counties;

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

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

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

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function addCountry(ShippingCountry $country): self
    {
        if (!$this->countries->contains($country)) {
            $country->addZone($this);
            $this->countries[] = $country;
        }

        return $this;
    }

    public function removeCountry(ShippingCountry $country): void
    {
        if ($this->countries->contains($country)) {
            $this->countries->removeElement($country);
            $country->removeZone($this);
        }
    }

    public function getCountries(): Collection
    {
        return $this->countries;
    }

    public function addCounty(ShippingCounty $county): self
    {
        if (!$this->counties->contains($county)) {
            $county->setZone($this);
            $this->counties[] = $county;
        }

        return $this;
    }

    public function removeCounty(ShippingCounty $county): void
    {
        if ($this->counties->contains($county)) {
            $this->counties->removeElement($county);
            $county->setZone(null);
        }
    }

    public function getCounties(): Collection
    {
        return $this->counties;
    }
}
