<?php
declare (strict_types=1);

namespace App\Entity\Billing;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Access\Access;
//use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;

/**
 * Classe ... qui ...
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ADMIN\') and object.getAccess().getOrganization().getId() == user.getOrganization().getId()'
        )
    ]
)]
//#[Auditable]
#[ORM\Entity]
class AccessBilling
{
    #[ORM\Column]
    #[ORM\Id]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\OneToOne(inversedBy: 'accessBilling', cascade: ['persist'], fetch: 'EAGER')]
    #[ORM\JoinColumn(nullable: true, onDelete: 'CASCADE')]
    private Access $access;

    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'accessBilling')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private FamilyQuotient $familyQuotient;

    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'accessBilling')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private ResidenceArea $residenceArea;

    #[ORM\OneToMany(mappedBy: 'accessBilling', targetEntity: BillPayment::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $billDetachedPayments;

    #[ORM\OneToMany(mappedBy: 'accessBilling', targetEntity: BillDebitBalance::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $billDebitBalances;

    #[ORM\OneToMany(mappedBy: 'accessBillingAccountBalanceReimbursement', targetEntity: BillPayment::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $accountBalanceReimbursements;

    public function __construct()
    {
        $this->billDetachedPayments = new ArrayCollection();
        $this->billDebitBalances = new ArrayCollection();
        $this->accountBalanceReimbursements = new ArrayCollection();
    }

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

    public function getFamilyQuotient(): ?FamilyQuotient
    {
        return $this->familyQuotient;
    }

    public function setFamilyQuotient(?FamilyQuotient $familyQuotient): self
    {
        $this->familyQuotient = $familyQuotient;
        return $this;
    }

    public function getResidenceArea(): ?ResidenceArea
    {
        return $this->residenceArea;
    }

    public function setResidenceArea(?ResidenceArea $residenceArea): self
    {
        $this->residenceArea = $residenceArea;
        return $this;
    }

    /**
     * @return Collection<int, BillPayment>
     */
    public function getBillDetachedPayments(): Collection
    {
        return $this->billDetachedPayments;
    }

    public function addBillDetachedPayment(BillPayment $billDetachedPayment): self
    {
        if (!$this->billDetachedPayments->contains($billDetachedPayment)) {
            $this->billDetachedPayments[] = $billDetachedPayment;
            $billDetachedPayment->setAccessBilling($this);
        }
        return $this;
    }

    public function removeBillDetachedPayment(BillPayment $billDetachedPayment): self
    {
        if ($this->billDetachedPayments->removeElement($billDetachedPayment)) {
            // set the owning side to null (unless already changed)
            if ($billDetachedPayment->getAccessBilling() === $this) {
                $billDetachedPayment->setAccessBilling(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, BillDebitBalance>
     */
    public function getBillDebitBalances(): Collection
    {
        return $this->billDebitBalances;
    }

    public function addBillDebitBalance(BillDebitBalance $billDebitBalance): self
    {
        if (!$this->billDebitBalances->contains($billDebitBalance)) {
            $this->billDebitBalances[] = $billDebitBalance;
            $billDebitBalance->setAccessBilling($this);
        }
        return $this;
    }

    public function removeBillDebitBalance(BillDebitBalance $billDebitBalance): self
    {
        if ($this->billDebitBalances->removeElement($billDebitBalance)) {
            // set the owning side to null (unless already changed)
            if ($billDebitBalance->getAccessBilling() === $this) {
                $billDebitBalance->setAccessBilling(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, BillPayment>
     */
    public function getAccountBalanceReimbursements(): Collection
    {
        return $this->accountBalanceReimbursements;
    }

    public function addAccountBalanceReimbursement(BillPayment $accountBalanceReimbursement): self
    {
        if (!$this->accountBalanceReimbursements->contains($accountBalanceReimbursement)) {
            $this->accountBalanceReimbursements[] = $accountBalanceReimbursement;
            $accountBalanceReimbursement->setAccessBillingAccountBalanceReimbursement($this);
        }
        return $this;
    }

    public function removeAccountBalanceReimbursement(BillPayment $accountBalanceReimbursement): self
    {
        if ($this->accountBalanceReimbursements->removeElement($accountBalanceReimbursement)) {
            // set the owning side to null (unless already changed)
            if ($accountBalanceReimbursement->getAccessBillingAccountBalanceReimbursement() === $this) {
                $accountBalanceReimbursement->setAccessBillingAccountBalanceReimbursement(null);
            }
        }
        return $this;
    }

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

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