<?php

declare(strict_types=1);

namespace App\Entity\Billing;

use App\Entity\Access\Access;
use App\Entity\Core\File;
use App\Entity\Core\Tagg;
use App\Entity\Organization\Organization;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Facture ou avoir.
 *
 * @see Bill, BillCredit, AdvancePayment
 *
 * @todo : A la suite de la migration, il faut supprimer le nom de la table pour avoir une table BillAccounting, et supprimer l'attribut discr.
 */
#[ORM\Entity]
#[ORM\Table(name: 'BillAccounting')]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
#[ORM\DiscriminatorMap(
    [
        'billaccounting' => BillAccounting::class,
        'bill' => Bill::class,
        'billcredit' => BillCredit::class,
        'advancepayment' => AdvancePayment::class,
    ]
)]
class AbstractBillAccounting
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    protected ?int $id = null;

    #[ORM\ManyToOne(targetEntity: Organization::class)]
    #[ORM\JoinColumn(nullable: true)]
    protected ?Organization $organization;

    #[ORM\ManyToOne(inversedBy: 'billCredits')]
    #[ORM\JoinColumn(nullable: true)]
    protected ?Bill $bill;

    /** @var Collection<int, BillingIntangibleExcludeDate> */
    #[ORM\OneToMany(targetEntity: BillingIntangibleExcludeDate::class, mappedBy: 'bill', cascade: ['persist'], orphanRemoval: true)]
    protected Collection $billingIntangibleExcludeDates;

    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'bills')]
    #[ORM\JoinColumn(nullable: true)]
    protected ?Pes $pes;

    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'bills')]
    #[ORM\JoinColumn(nullable: true)]
    protected ?BergerLevrault $bergerLevrault;

    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'bills')]
    #[ORM\JoinColumn(nullable: true)]
    protected ?Ciril $ciril;

    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'bills')]
    #[ORM\JoinColumn(nullable: true)]
    protected ?Jvs $jvs;

    /** @var Collection<int, Tagg> */
    #[ORM\ManyToMany(targetEntity: Tagg::class, inversedBy: 'billAccountings', cascade: ['persist'])]
    #[ORM\JoinTable(name: 'tag_billAccounting')]
    #[ORM\JoinColumn(name: 'billAccounting_id', referencedColumnName: 'id')]
    #[ORM\InverseJoinColumn(name: 'tag_id', referencedColumnName: 'id')]
    protected Collection $tags;

    #[ORM\ManyToOne(targetEntity: Access::class, cascade: [], inversedBy: 'bills')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: false, onDelete: 'SET NULL')]
    protected Access $access;

    #[ORM\OneToOne(targetEntity: BillAccessDetail::class, cascade: ['persist'])]
    protected ?BillAccessDetail $accessDetail;

    #[ORM\OneToOne(targetEntity: BillPeriod::class, cascade: ['persist'])]
    protected ?BillPeriod $billPeriod;

    #[ORM\OneToOne(targetEntity: BillTotalDetail::class, cascade: ['persist'])]
    protected ?BillTotalDetail $totalDetail;

    #[ORM\OneToOne(targetEntity: File::class, cascade: ['persist'])]
    protected ?File $file;

    #[ORM\ManyToOne(targetEntity: Odyssee::class, cascade: ['persist'], inversedBy: 'bills')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: false, onDelete: 'SET NULL')]
    protected Odyssee $odyssee; // TODO: sûr que c'est pas nullable?

    #[ORM\ManyToOne(targetEntity: Afi::class, cascade: ['persist'], inversedBy: 'bills')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: false, onDelete: 'SET NULL')]
    protected Afi $afi;

    #[ORM\ManyToOne(targetEntity: CirilCivil::class, cascade: ['persist'], inversedBy: 'bills')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: false, onDelete: 'SET NULL')]
    protected CirilCivil $cirilCivil;

    public function __construct()
    {
        $this->billingIntangibleExcludeDates = new ArrayCollection();
        $this->tags = new ArrayCollection();
    }

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

    public function getOrganization(): ?Organization
    {
        return $this->organization;
    }

    public function setOrganization(?Organization $organization): self
    {
        $this->organization = $organization;

        return $this;
    }

    public function getBill(): ?Bill
    {
        return $this->bill;
    }

    public function setBill(?Bill $bill): self
    {
        $this->bill = $bill;

        return $this;
    }

    /**
     * @return Collection<int, BillingIntangibleExcludeDate>
     */
    public function getBillingIntangibleExcludeDates(): Collection
    {
        return $this->billingIntangibleExcludeDates;
    }

    public function addBillingIntangibleExcludeDate(BillingIntangibleExcludeDate $billingIntangibleExcludeDate): self
    {
        if (!$this->billingIntangibleExcludeDates->contains($billingIntangibleExcludeDate)) {
            $this->billingIntangibleExcludeDates[] = $billingIntangibleExcludeDate;
            $billingIntangibleExcludeDate->setBill($this);
        }

        return $this;
    }

    public function removeBillingIntangibleExcludeDate(BillingIntangibleExcludeDate $billingIntangibleExcludeDate): self
    {
        if ($this->billingIntangibleExcludeDates->removeElement($billingIntangibleExcludeDate)) {
            // set the owning side to null (unless already changed)
            if ($billingIntangibleExcludeDate->getBill() === $this) {
                $billingIntangibleExcludeDate->setBill(null);
            }
        }

        return $this;
    }

    public function getPes(): ?Pes
    {
        return $this->pes;
    }

    public function setPes(?Pes $pes): self
    {
        $this->pes = $pes;

        return $this;
    }

    public function getBergerLevrault(): ?BergerLevrault
    {
        return $this->bergerLevrault;
    }

    public function setBergerLevrault(?BergerLevrault $bergerLevrault): self
    {
        $this->bergerLevrault = $bergerLevrault;

        return $this;
    }

    public function getCiril(): ?Ciril
    {
        return $this->ciril;
    }

    public function setCiril(?Ciril $ciril): self
    {
        $this->ciril = $ciril;

        return $this;
    }

    public function getJvs(): ?Jvs
    {
        return $this->jvs;
    }

    public function setJvs(?Jvs $jvs): self
    {
        $this->jvs = $jvs;

        return $this;
    }

    /**
     * @return Collection<int, Tagg>
     */
    public function getTags(): Collection
    {
        return $this->tags;
    }

    public function addTag(Tagg $tag): self
    {
        if (!$this->tags->contains($tag)) {
            $this->tags[] = $tag;
            $tag->addBillAccounting($this);
        }

        return $this;
    }

    public function removeTag(Tagg $tag): self
    {
        if ($this->tags->removeElement($tag)) {
            $tag->removeBillAccounting($this);
        }

        return $this;
    }

    public function getAccess(): Access
    {
        return $this->access;
    }

    public function setAccess(Access $access): self
    {
        $this->access = $access;

        return $this;
    }

    public function getAccessDetail(): BillAccessDetail
    {
        return $this->accessDetail;
    }

    public function setAccessDetail(BillAccessDetail $accessDetail): self
    {
        $this->accessDetail = $accessDetail;

        return $this;
    }

    public function getBillPeriod(): BillPeriod
    {
        return $this->billPeriod;
    }

    public function setBillPeriod(BillPeriod $billPeriod): self
    {
        $this->billPeriod = $billPeriod;

        return $this;
    }

    public function getTotalDetail(): BillTotalDetail
    {
        return $this->totalDetail;
    }

    public function setTotalDetail(BillTotalDetail $totalDetail): self
    {
        $this->totalDetail = $totalDetail;

        return $this;
    }

    public function getFile(): File
    {
        return $this->file;
    }

    public function setFile(File $file): self
    {
        $this->file = $file;

        return $this;
    }

    public function getOdyssee(): Odyssee
    {
        return $this->odyssee;
    }

    public function setOdyssee(Odyssee $odyssee): self
    {
        $this->odyssee = $odyssee;

        return $this;
    }

    public function getAfi(): Afi
    {
        return $this->afi;
    }

    public function setAfi(Afi $afi): self
    {
        $this->afi = $afi;

        return $this;
    }

    public function getCirilCivil(): CirilCivil
    {
        return $this->cirilCivil;
    }

    public function setCirilCivil(CirilCivil $cirilCivil): self
    {
        $this->cirilCivil = $cirilCivil;

        return $this;
    }
}
