<?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\SoftDeletable;
use CpCreation\VitiCore\Behavior\Impl\Translatable;
use CpCreation\VitiCore\Behavior\IsSoftDeletable;
use CpCreation\VitiCore\Product\Model\ProductPriceGroup;
use CpCreation\VitiCore\Product\Model\ProductVariant;
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\DeliveryMethodRepository')]
#[ORM\Table(name: 'shop_delivery_method')]
#[ORM\HasLifecycleCallbacks]
class DeliveryMethod implements HasTranslatable, HasDisable, IsSoftDeletable
{
    use Disable;
    use SoftDeletable;
    use Translatable;

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

    #[ORM\Column(type: 'datetime', nullable: true)]
    private ?\DateTime $unPublishedAt = null;

    #[ORM\Column(type: 'datetime', nullable: true)]
    #[Assert\NotBlank]
    private ?\DateTime $publishedAt = 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' => 0])]
    private bool $multipleVariant;

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

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

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

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

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

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

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

    #[ORM\ManyToOne(targetEntity: DeliveryMethodCategory::class, inversedBy: 'methods')]
    #[ORM\JoinColumn(name: 'delivery_category_id', referencedColumnName: 'id', nullable: true)]
    #[Gedmo\SortableGroup]
    #[Assert\NotBlank]
    private ?DeliveryMethodCategory $category = null;

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

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

    #[ORM\OneToMany(targetEntity: ShippingPrice::class, mappedBy: 'deliveryMethod')]
    private Collection $shippingPrices;

    #[ORM\ManyToMany(targetEntity: ProductVariant::class)]
    #[ORM\OrderBy(['product' => 'ASC', 'position' => 'ASC'])]
    private Collection $productVariants;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
        $this->disabled = false;
        $this->viewCounties = false;
        $this->disabledEquivalence = false;
        $this->enabledShippingPrice = false;
        $this->viewProduct = false;
        $this->depot = false;
        $this->multipleVariant = false;
        $this->counties = new ArrayCollection();
        $this->groups = new ArrayCollection();
        $this->shippingPrices = new ArrayCollection();
        $this->productVariants = new ArrayCollection();
    }

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

    public function getUnPublishedAt(): ?\DateTime
    {
        return $this->unPublishedAt;
    }

    public function setUnPublishedAt(?\DateTime $unPublishedAt = null): void
    {
        $this->unPublishedAt = $unPublishedAt;
    }

    public function getPublishedAt(): ?\DateTime
    {
        return $this->publishedAt;
    }

    public function setPublishedAt(?\DateTime $publishedAt = null): void
    {
        $this->publishedAt = $publishedAt;
    }

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

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

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

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

    public function isDisabledEquivalence(): bool
    {
        return $this->disabledEquivalence;
    }

    public function setDisabledEquivalence(bool $disabledEquivalence): void
    {
        $this->disabledEquivalence = $disabledEquivalence;
    }

    public function isEnabledShippingPrice(): bool
    {
        return $this->enabledShippingPrice;
    }

    public function setEnabledShippingPrice(bool $enabledShippingPrice): void
    {
        $this->enabledShippingPrice = $enabledShippingPrice;
    }

    public function isViewCounties(): bool
    {
        return $this->viewCounties;
    }

    public function setViewCounties(bool $viewCounties): void
    {
        $this->viewCounties = $viewCounties;
    }

    public function getPosition(): ?int
    {
        return $this->position;
    }

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

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

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

    public function isViewProduct(): bool
    {
        return $this->viewProduct;
    }

    public function setViewProduct(bool $viewProduct): void
    {
        $this->viewProduct = $viewProduct;
    }

    public function isDepot(): bool
    {
        return $this->depot;
    }

    public function setDepot(bool $depot): void
    {
        $this->depot = $depot;
    }

    public function isMultipleVariant(): bool
    {
        return $this->multipleVariant;
    }

    public function setMultipleVariant(bool $multipleVariant): void
    {
        $this->multipleVariant = $multipleVariant;
    }

    public function getCategory(): ?DeliveryMethodCategory
    {
        return $this->category;
    }

    public function setCategory(?DeliveryMethodCategory $category = null): void
    {
        $this->category = $category;
    }

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

    public function getShippingPrices(): Collection
    {
        return $this->shippingPrices;
    }

    public function addProductVariant(ProductVariant $productVariant): self
    {
        $this->productVariants[] = $productVariant;

        return $this;
    }

    public function removeProductVariant(ProductVariant $productVariant): void
    {
        $this->productVariants->removeElement($productVariant);
    }

    public function getProductVariants(): Collection
    {
        return $this->productVariants;
    }

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