<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (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\Product\Model;

use CpCreation\VitiCore\Behavior\HasTimestamp;
use CpCreation\VitiCore\Behavior\Impl\SoftDeletable;
use CpCreation\VitiCore\Behavior\Impl\Timestamp;
use CpCreation\VitiCore\Behavior\IsSoftDeletable;
use CpCreation\VitiCore\Product\Repository\ProductPriceRepository;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: ProductPriceRepository::class)]
#[ORM\Table(name: 'product_price')]
#[ORM\HasLifecycleCallbacks]
class ProductPrice implements HasTimestamp, IsSoftDeletable
{
    use SoftDeletable;
    use Timestamp;

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

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

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

    #[ORM\ManyToOne(targetEntity: ProductPriceGroup::class, inversedBy: 'prices')]
    #[Assert\NotBlank]
    private ?ProductPriceGroup $group = null;

    #[ORM\ManyToOne(targetEntity: ProductVariant::class, inversedBy: 'prices')]
    #[ORM\JoinColumn(name: 'variant_id', referencedColumnName: 'id')]
    private ?ProductVariant $variant = null;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
    }

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

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

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

    public function getPriceDiscount(): ?int
    {
        return $this->priceDiscount;
    }

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

    public function getGroup(): ?ProductPriceGroup
    {
        return $this->group;
    }

    public function setGroup(?ProductPriceGroup $group): void
    {
        $this->group = $group;
    }

    public function getVariant(): ?ProductVariant
    {
        return $this->variant;
    }

    public function setVariant(?ProductVariant $variant): void
    {
        $this->variant = $variant;
    }

    public function getFinalPrice(): int
    {
        if ($this->priceDiscount > 0 && $this->priceDiscount < $this->price) {
            return $this->priceDiscount;
        }

        return $this->price;
    }
}
