<?php
declare (strict_types=1);

namespace App\Entity\Core;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Organization\Organization;
use App\Entity\Person\Person;
use App\Repository\Core\BankAccountRepository;
//use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Données bancaire d'une Person ou d'une Organization
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted("BANK_ACCOUNT_READ", object)'
        ),
        new Put(
            security: 'is_granted("BANK_ACCOUNT_EDIT", object)'
        ),
        new Delete(
            security: 'is_granted("BANK_ACCOUNT_DELETE", object)'
        ),
        new Post(),
        new GetCollection()
    ]
)]
//#[Auditable]
#[ORM\Entity(repositoryClass: BankAccountRepository::class)]
class BankAccount
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $bankName = null;

    #[ORM\Column(length: 11, nullable: true)]
    #[Assert\Bic(message: 'invalid_bic')]
    private ?string $bic = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $bicInvalid = null;

    #[ORM\Column(length: 34, nullable: true)]
    #[Assert\Iban(message: 'invalid_iban')]
    private ?string $iban = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $ibanInvalid = null;

    #[ORM\Column(options: ['default' => false])]
    private bool $holderIdDifferent = false;
    /**
     * 0 => jamais facturé, 1 => facturé 1 fois, 2 => facturé plusieurs fois
     */
    #[ORM\Column(options: ['default' => 0])]
    private ?int $countInvoiced = 0;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $holder = null;

    #[ORM\Column(options: ['default' => false])]
    #[Assert\NotNull]
    private bool $principal = false;

    #[ORM\Column(type: 'text', nullable: true)]
    private ?string $debitAddress = null;

    #[ORM\Column(length: 35, unique: true, nullable: true)]
    private ?string $rum = null;

    #[ORM\Column(type: 'date', nullable: true)]
    private ?\DateTimeInterface $signatureDateSamplingMandate;

    #[ORM\ManyToMany(targetEntity: Organization::class, mappedBy: 'bankAccounts', cascade: ['persist'])]
    private Collection $organization;

    #[ORM\ManyToMany(targetEntity: Person::class, mappedBy: 'bankAccount', cascade: ['persist'])]
    private Collection $person;

    #[Pure]
    public function __construct()
    {
        $this->organization = new ArrayCollection();
        $this->person = new ArrayCollection();
    }

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

    public function getBankName(): ?string
    {
        return $this->bankName;
    }

    public function setBankName(?string $bankName): self
    {
        $this->bankName = $bankName;
        return $this;
    }

    public function getBic(): ?string
    {
        return $this->bic;
    }

    public function setBic(?string $bic): self
    {
        $this->bic = $bic;
        return $this;
    }

    public function getBicInvalid(): ?string
    {
        return $this->bicInvalid;
    }

    public function setBicInvalid(?string $bicInvalid): self
    {
        $this->bicInvalid = $bicInvalid;
        return $this;
    }

    public function getIban(): ?string
    {
        return $this->iban;
    }

    public function setIban(?string $iban): self
    {
        $this->iban = $iban;
        return $this;
    }

    public function getIbanInvalid(): ?string
    {
        return $this->ibanInvalid;
    }

    public function setIbanInvalid(?string $ibanInvalid): self
    {
        $this->ibanInvalid = $ibanInvalid;
        return $this;
    }

    public function getHolderIdDifferent(): bool
    {
        return $this->holderIdDifferent;
    }

    public function setHolderIdDifferent(bool $holderIdDifferent): self
    {
        $this->holderIdDifferent = $holderIdDifferent;
        return $this;
    }

    public function getCountInvoiced(): int
    {
        return $this->countInvoiced;
    }

    public function setCountInvoiced(int $countInvoiced): self
    {
        $this->countInvoiced = $countInvoiced;
        return $this;
    }

    public function getHolder(): ?string
    {
        return $this->holder;
    }

    public function setHolder(?string $holder): self
    {
        $this->holder = $holder;
        return $this;
    }

    public function getPrincipal(): bool
    {
        return $this->principal;
    }

    public function setPrincipal(bool $principal): self
    {
        $this->principal = $principal;
        return $this;
    }

    public function getDebitAddress(): ?string
    {
        return $this->debitAddress;
    }

    public function setDebitAddress(?string $debitAddress): self
    {
        $this->debitAddress = $debitAddress;
        return $this;
    }

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

    public function addOrganization(Organization $organization): self
    {
        if (!$this->organization->contains($organization)) {
            $this->organization[] = $organization;
            $organization->addBankAccount($this);
        }
        return $this;
    }

    public function removeOrganization(Organization $organization): self
    {
        if ($this->organization->removeElement($organization)) {
            $organization->removeBankAccount($this);
        }
        return $this;
    }

    public function getRum(): ?string
    {
        return $this->rum;
    }

    public function setRum(?string $rum): self
    {
        $this->rum = $rum;
        return $this;
    }

    public function getSignatureDateSamplingMandate(): ?\DateTimeInterface
    {
        return $this->signatureDateSamplingMandate;
    }

    public function setSignatureDateSamplingMandate(?\DateTimeInterface $signatureDateSamplingMandate): self
    {
        $this->signatureDateSamplingMandate = $signatureDateSamplingMandate;
        return $this;
    }

    /**
     * @return Collection<int, Person>
     */
    public function getPerson(): Collection
    {
        return $this->person;
    }

    public function addPerson(Person $person): self
    {
        if (!$this->person->contains($person)) {
            $this->person[] = $person;
            $person->addBankAccount($this);
        }
        return $this;
    }

    public function removePerson(Person $person): self
    {
        if ($this->person->removeElement($person)) {
            $person->removeBankAccount($this);
        }
        return $this;
    }
}
