<?php


declare(strict_types=1);

/*
 * 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 CpCreation\VitiCore\Product\Model\ProductType;
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\ShippingPriceRepository')]
#[ORM\Table(name: 'shop_shipping_price')]
#[ORM\HasLifecycleCallbacks]
class ShippingPrice 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')]
    #[Assert\NotBlank]
    private ?int $qtyAt = null;

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

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

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

    #[ORM\Column(type: 'decimal', precision: 5, scale: 4, nullable: true)]
    private ?string $privateMargePercent = null;

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

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

    #[ORM\ManyToOne(targetEntity: DeliveryMethod::class, inversedBy: 'shippingPrices')]
    #[ORM\JoinColumn(name: 'delivery_method_id', referencedColumnName: 'id', nullable: true)]
    private ?DeliveryMethod $deliveryMethod = null;

    /**
     * @var Collection<int, ShippingZone>
     */
    #[ORM\ManyToMany(targetEntity: ShippingZone::class)]
    #[Assert\Count(min: 1, minMessage: 'Vous devez ajouter au moins une zone')]
    private Collection $zones;

    /**
     * @var Collection<int, ProductType>
     */
    #[ORM\ManyToMany(targetEntity: ProductType::class)]
    #[Assert\Count(min: 1, minMessage: 'Vous devez ajouter au moins un type')]
    private Collection $types;

    /**
     * @var Collection<int, ProductPriceGroup>
     */
    #[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->zones = new ArrayCollection();
        $this->types = new ArrayCollection();
        $this->groups = new ArrayCollection();
        $this->configGlobal = false;
    }

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

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

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

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

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

    public function getPackagePrice(): ?int
    {
        return $this->packagePrice;
    }

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

    public function getBottlePrice(): ?int
    {
        return $this->bottlePrice;
    }

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

    public function isConfigGlobal(): bool
    {
        return $this->configGlobal;
    }

    public function setConfigGlobal(bool $configGlobal): void
    {
        $this->configGlobal = $configGlobal;
    }

    public function getPrivateMargePercent(): ?float
    {
        return null !== $this->privateMargePercent ? (float) $this->privateMargePercent : null;
    }

    public function setPrivateMargePercent(?float $privateMargePercent = null): void
    {
        $this->privateMargePercent = null !== $privateMargePercent ? (string) $privateMargePercent : null;
    }

    public function getPrivateMarge(): ?int
    {
        return $this->privateMarge;
    }

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

    public function getNameCarrier(): ?string
    {
        return $this->nameCarrier;
    }

    public function setNameCarrier(?string $nameCarrier): void
    {
        $this->nameCarrier = $nameCarrier;
    }

    public function getDeliveryMethod(): ?DeliveryMethod
    {
        return $this->deliveryMethod;
    }

    public function setDeliveryMethod(?DeliveryMethod $deliveryMethod): void
    {
        $this->deliveryMethod = $deliveryMethod;
    }

    public function addType(ProductType $productType): self
    {
        if (!$this->types->contains($productType)) {
            $this->types->add($productType);
        }

        return $this;
    }

    public function removeType(ProductType $productType): void
    {
        $this->types->removeElement($productType);
    }

    /**
     * @return Collection<int, ProductType>
     */
    public function getTypes(): Collection
    {
        return $this->types;
    }

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

        return $this;
    }

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

    /**
     * @return Collection<int, ShippingZone>
     */
    public function getZones(): Collection
    {
        return $this->zones;
    }

    public function addGroup(ProductPriceGroup $group): self
    {
        if (!$this->groups->contains($group)) {
            $this->groups->add($group);
        }

        return $this;
    }

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

    /**
     * @return Collection<int, ProductPriceGroup>
     */
    public function getGroups(): Collection
    {
        return $this->groups;
    }

    #[Assert\Callback]
    public function validate(ExecutionContextInterface $context): void
    {
        if (null === $this->getPackagePrice() && null === $this->getBottlePrice()) {
            $context->buildViolation('Veuillez ajouter un prix')
                ->atPath('bottlePrice')
                ->addViolation();
        }
    }
}
