<?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\HasTimestamp;
use CpCreation\VitiCore\Behavior\Impl\Timestamp;
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\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

#[ORM\Entity(repositoryClass: 'CpCreation\VitiCore\Shop\Repository\ShippingAdditionalRepository')]
#[ORM\Table(name: 'shop_shipping_additional')]
#[ORM\HasLifecycleCallbacks]
class ShippingAdditional implements HasTimestamp
{
    use Timestamp;

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

    #[ORM\Column(type: 'integer')]
    #[Assert\NotBlank]
    private ?int $qtyOf = null;

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

    #[ORM\Column(type: 'integer')]
    private ?int $dateOf = null;

    #[ORM\Column(type: 'integer')]
    private ?int $dateAt = null;

    #[ORM\Column(type: 'integer')]
    private ?int $price = null;

    #[ORM\ManyToMany(targetEntity: ShippingCounty::class)]
    #[ORM\JoinTable(name: 'shop_shipping_additional_counties')]
    #[ORM\JoinColumn(name: 'shipping_additional_id', referencedColumnName: 'id')]
    #[ORM\InverseJoinColumn(name: 'countie_id', referencedColumnName: 'id')]
    #[ORM\OrderBy(['code' => 'ASC'])]
    #[Assert\Valid]
    private Collection $counties;

    #[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->counties = new ArrayCollection();
        $this->groups = new ArrayCollection();
    }

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

    public function getQtyOf(): ?int
    {
        return $this->qtyOf;
    }

    public function setQtyOf(?int $qtyOf = null): void
    {
        $this->qtyOf = $qtyOf;
    }

    public function getQtyAt(): ?int
    {
        return $this->qtyAt;
    }

    public function setQtyAt(?int $qtyAt = null): void
    {
        $this->qtyAt = $qtyAt;
    }

    public function getDateOf(): ?int
    {
        return $this->dateOf;
    }

    public function setDateOf(?int $dateOf = null): void
    {
        $this->dateOf = $dateOf;

        // Correspond au 31 décembre
        if (!$dateOf) {
            $this->dateOf = 1231;
        }
    }

    public function getDateAt(): ?int
    {
        return $this->dateAt;
    }

    public function setDateAt(?int $dateAt = null): void
    {
        $this->dateAt = $dateAt;

        // Correspond au premier janvier
        if (!$dateAt) {
            $this->dateAt = 101;
        }
    }

    public function getPrice(): ?int
    {
        return $this->price;
    }

    public function setPrice(int $price): void
    {
        $this->price = $price;
    }

    public function addCounty(ShippingCounty $county): self
    {
        $this->counties[] = $county;

        return $this;
    }

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

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

    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;
    }

    #[Assert\Callback]
    public function validate(ExecutionContextInterface $context): void
    {
        if (null !== $this->getQtyAt() && null !== $this->getQtyOf() && $this->getQtyAt() <= $this->getQtyOf()) {
            $context->buildViolation('La quantité maximale doit être supérieure à la quantité minimale.')
                ->atPath('qtyAt')
                ->addViolation();
        }
    }
}
