<?php

namespace AppBundle\Entity\Billing;

use AppBundle\Entity\Core\Tagg;
use AppBundle\Entity\Organization\Organization;
use AppBundle\Entity\Product\Intangible;
use AppBundle\Entity\Traits\ActivityPeriodTrait;
use AppBundle\Entity\Traits\BillingRuleTrait;
use AppBundle\Entity\Traits\LockableTrait;
use AppBundle\Enum\Billing\PeriodicityTypeEnum;
use Doctrine\ORM\Mapping as ORM;
use Dunglas\ApiBundle\Annotation\Iri;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use AppBundle\Annotation\DefaultField;
use AppBundle\Entity\Traits\TimestampableEntity;
use AppBundle\Entity\Traits\CreatorUpdaterEntity;

/**
 * Enregistrement d'un produit à facturer par une Organization, un Access ou un EducationalProject
 * Classe de base de @see AccessIntangible, EducationalProjectIntangible
 *
 *
 */
#[ORM\Entity]
#[ORM\Table(name: 'BillingIntangible')]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
#[ORM\DiscriminatorMap(['access' => 'AccessIntangible', 'educationalproject' => 'EducationalProjectIntangible'])]
abstract class AbstractBillingIntangible
{
    use TimestampableEntity;
    use CreatorUpdaterEntity;
    use ActivityPeriodTrait;
    use BillingRuleTrait;
    /**
     * @var int
     */
    #[ORM\Column(type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[Groups(['accessintangible', 'educationalprojectintangible', 'access_details', 'student_registration', 'educationalproject_details', 'accessbilling_edit'])]
    private $id;
    /**
     * @var Intangible
     */
    #[ORM\ManyToOne(targetEntity: 'AppBundle\Entity\Product\Intangible', inversedBy: 'accessIntangibles')]
    #[ORM\JoinColumn(nullable: false)]
    #[Assert\NotNull]
    #[Groups(['accessintangible', 'accessintangible_intangible', 'educationalprojectintangible', 'access_details_accessintangibles', 'student_registration_accessintangibles', 'educationalproject_details_educationalprojectintangibles', 'accessbilling_edit_accessintangibles', 'access_intangible_list'])]
    private $intangible;
    /**
     * @var string
     *
     * @Iri("https://schema.org/description")
     */
    #[ORM\Column(type: 'text', nullable: true)]
    #[Assert\Type(type: 'string')]
    #[Groups(['accessintangible', 'educationalprojectintangible', 'student_registration_accessintangibles', 'accessbilling_edit_accessintangibles', 'access_intangible_list'])]
    private $customDescription;
    /**
     * @var int
     */
    #[ORM\Column(type: 'integer', nullable: true)]
    #[Assert\Type(type: 'integer', message: 'invalid-integer')]
    #[Groups(['accessintangible', 'educationalprojectintangible', 'student_registration_accessintangibles', 'accessbilling_edit_accessintangibles'])]
    private $quantity;
    /**
     * @var float
     */
    #[ORM\Column(type: 'float', nullable: true)]
    #[Assert\Type(type: 'float')]
    #[Groups(['accessintangible', 'educationalprojectintangible', 'student_registration_accessintangibles', 'accessbilling_edit_accessintangibles'])]
    private $forcedAmount;
    /**
     * @var bool
     */
    #[ORM\Column(type: 'boolean', options: ['default' => false])]
    #[Assert\Type(type: 'boolean')]
    #[Assert\NotNull]
    #[Groups(['accessintangible', 'educationalprojectintangible', 'student_registration_accessintangibles', 'accessbilling_edit_accessintangibles'])]
    private $isForcedAmount = false;
    /**
     * @var ArrayCollection<Tagg>
     */
    #[ORM\ManyToMany(targetEntity: 'AppBundle\Entity\Core\Tagg', cascade: ['persist'], inversedBy: 'billingIntangibles')]
    #[Assert\Valid]
    #[ORM\JoinTable(name: 'tag_billingIntangible', joinColumns: [], inverseJoinColumns: [])]
    #[ORM\JoinColumn(name: 'billingIntangible_id', referencedColumnName: 'id')]
    #[ORM\JoinColumn(name: 'tag_id', referencedColumnName: 'id')]
    #[Groups(['billingintangible_tags', 'manage_tags', 'access_intangible_list'])]
    private $tags;
    /**
     * The constructor
     */
    public function __construct() {
        $this->tags= new ArrayCollection();
    }
    /**
     * Sets id.
     *
     * @param int $id
     *
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }
    /**
     * Gets id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set customDescription
     *
     * @param string $customDescription
     *
     * @return AbstractBillingIntangible
     */
    public function setCustomDescription($customDescription)
    {
        $this->customDescription = $customDescription;

        return $this;
    }
    /**
     * Get customDescription
     *
     * @return string
     */
    public function getCustomDescription()
    {
        return $this->customDescription;
    }
    /**
     * Set quantity
     *
     * @param integer $quantity
     *
     * @return AbstractBillingIntangible
     */
    public function setQuantity($quantity)
    {
        $this->quantity = $quantity;

        return $this;
    }
    /**
     * Get quantity
     *
     * @return integer
     */
    public function getQuantity()
    {
        return $this->quantity;
    }
    /**
     * Set forcedAmount
     *
     * @param float $forcedAmount
     *
     * @return AbstractBillingIntangible
     */
    public function setForcedAmount($forcedAmount)
    {
        $this->forcedAmount = floatval($forcedAmount);

        return $this;
    }
    /**
     * Get forcedAmount
     *
     * @return float
     */
    public function getForcedAmount()
    {
        return $this->forcedAmount;
    }
    /**
     * Set isForcedAmount
     *
     * @param boolean $isForcedAmount
     *
     * @return AbstractBillingIntangible
     */
    public function setIsForcedAmount($isForcedAmount)
    {
        $this->isForcedAmount = $isForcedAmount;

        return $this;
    }
    /**
     * Get isForcedAmount
     *
     * @return boolean
     */
    public function getIsForcedAmount()
    {
        return $this->isForcedAmount;
    }
    /**
     * Set intangible
     *
     * @param \AppBundle\Entity\Product\Intangible $intangible
     *
     * @return AbstractBillingIntangible
     */
    public function setIntangible(\AppBundle\Entity\Product\Intangible $intangible)
    {
        $this->intangible = $intangible;

        return $this;
    }
    /**
     * Get intangible
     *
     * @return \AppBundle\Entity\Product\Intangible
     */
    public function getIntangible()
    {
        return $this->intangible;
    }
    /**
     * Add tag
     *
     * @param \AppBundle\Entity\Core\Tagg $tag
     *
     * @return AbstractBillingIntangible
     */
    public function addTag(\AppBundle\Entity\Core\Tagg $tag)
    {
        $this->tags[] = $tag;

        return $this;
    }
    /**
     * Remove tag
     *
     * @param \AppBundle\Entity\Core\Tagg $tag
     */
    public function removeTag(\AppBundle\Entity\Core\Tagg $tag)
    {
        $this->tags->removeElement($tag);
    }
    /**
     * Get tags
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTags()
    {
        return $this->tags;
    }
    /**
     * Sets start date
     *
     * @param \DateTime $startDate
     * @return $this
     */
    public function setStartDate(\DateTime $startDate = null) {

        if($startDate == null) $startDate = new \DateTime();
        $this->startDate = $startDate;
        if(!empty($this->getIntangible()) && $this->getIntangible()->getBillingPeriodicityType() === PeriodicityTypeEnum::PUNCTUAL){
            $this->setEndDate($startDate);
        }

        return $this;
    }
    public function setEndDate(\DateTime $endDate = null) {
        if(empty($endDate) && !empty($this->getIntangible()) && $this->getIntangible()->getBillingPeriodicityType() === PeriodicityTypeEnum::PUNCTUAL){
            return $this;
        }
        $this->endDate = $endDate;
        return $this;
    }
}
