<?php

namespace App\PaymentMethod\Entity;

use App\Auth\Entity\User;
use App\PaymentMethod\Repository\BankAccountRepository;
use App\Shared\Tenant\NullableTenantOwnedTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

/**
 * A bank account used as a payment method. Referenced from patient invoices to
 * record which account a service payment was made to. This entity only stores
 * the account info; the payment linkage lives on the invoice side.
 *
 * حساب مالِ **محیط** است، نه کاربر: پزشکی که هم مطب شخصی دارد و هم کلینیک،
 * حساب‌هایشان جداست. `user_id` می‌ماند تا بدانیم چه کسی ثبتش کرده، ولی اسکوپِ
 * خواندن و ویرایش، محیط است. حساب‌هایی که از پیش از فاز ۶ مانده‌اند و مالکشان
 * چند محیط دارد، محیطشان تهی است تا خودش تعیین کند.
 */
#[ORM\Entity(repositoryClass: BankAccountRepository::class)]
#[ORM\Table(name: 'bank_accounts')]
#[ORM\Index(columns: ['user_id'], name: 'idx_bank_accounts_user')]
#[ORM\Index(columns: ['entity_type', 'entity_id'], name: 'idx_bank_accounts_entity')]
class BankAccount
{
    use NullableTenantOwnedTrait;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 36, unique: true)]
    private string $uuid;

    #[ORM\ManyToOne(targetEntity: User::class)]
    #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false, onDelete: 'RESTRICT')]
    private User $user;

    #[ORM\Column(name: 'bank_name', type: 'string', length: 100)]
    private string $bankName;

    #[ORM\Column(name: 'card_number', type: 'string', length: 32, nullable: true)]
    private ?string $cardNumber = null;

    #[ORM\Column(name: 'account_number', type: 'string', length: 64)]
    private string $accountNumber;

    #[ORM\Column(name: 'shaba_number', type: 'string', length: 34, nullable: true)]
    private ?string $shabaNumber = null;

    #[ORM\Column(name: 'is_active', type: 'boolean')]
    private bool $isActive = true;

    #[ORM\Column(name: 'created_at', type: 'integer')]
    private int $createdAt;

    #[ORM\Column(name: 'updated_at', type: 'integer')]
    private int $updatedAt;

    public function __construct(
        User $user,
        string $bankName,
        string $accountNumber,
        ?string $cardNumber = null,
        ?string $shabaNumber = null,
    ) {
        $this->uuid          = Uuid::v4()->toRfc4122();
        $this->user          = $user;
        $this->bankName      = $bankName;
        $this->accountNumber = $accountNumber;
        $this->cardNumber    = $cardNumber ?: null;
        $this->shabaNumber   = $shabaNumber ?: null;
        $this->createdAt     = time();
        $this->updatedAt     = time();
    }

    public function getId(): ?int              { return $this->id; }
    public function getUuid(): string          { return $this->uuid; }
    public function getUser(): User            { return $this->user; }
    public function getBankName(): string      { return $this->bankName; }
    public function getCardNumber(): ?string   { return $this->cardNumber; }
    public function getAccountNumber(): string { return $this->accountNumber; }
    public function getShabaNumber(): ?string  { return $this->shabaNumber; }
    public function isActive(): bool           { return $this->isActive; }

    public function setBankName(string $v): self      { $this->bankName = $v; $this->touch(); return $this; }
    public function setCardNumber(?string $v): self   { $this->cardNumber = $v ?: null; $this->touch(); return $this; }
    public function setAccountNumber(string $v): self { $this->accountNumber = $v; $this->touch(); return $this; }
    public function setShabaNumber(?string $v): self  { $this->shabaNumber = $v ?: null; $this->touch(); return $this; }
    public function setActive(bool $v): self          { $this->isActive = $v; $this->touch(); return $this; }

    private function touch(): void { $this->updatedAt = time(); }

    public function toArray(): array
    {
        return [
            'uuid'           => $this->uuid,
            'bank_name'      => $this->bankName,
            'card_number'    => $this->cardNumber,
            'account_number' => $this->accountNumber,
            'shaba_number'   => $this->shabaNumber,
            'is_active'      => $this->isActive,
            'created_at'     => $this->createdAt,
            // تهی یعنی «محیطش هنوز تعیین نشده»؛ پنل با همین نشانه‌گذاری‌اش می‌کند.
            'entity_type'    => $this->entityType,
        ];
    }
}
