<?php

namespace App\PaymentMethod\Entity;

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

/**
 * A card reader (POS) device used as a payment method. Referenced from patient
 * invoices to record which device a service payment was collected on.
 *
 * قرینهٔ {@see BankAccount}: دستگاه مالِ محیط است نه کاربر، و ردیف‌های مبهمِ
 * پیش از فاز ۶ محیط تهی دارند تا مالک خودش تعیین کند.
 */
#[ORM\Entity(repositoryClass: PosRepository::class)]
#[ORM\Table(name: 'pos_devices')]
#[ORM\Index(columns: ['user_id'], name: 'idx_pos_devices_user')]
#[ORM\Index(columns: ['entity_type', 'entity_id'], name: 'idx_pos_devices_entity')]
class Pos
{
    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: 'serial_number', type: 'string', length: 64, nullable: true)]
    private ?string $serialNumber = null;

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

    #[ORM\Column(name: 'account_number', type: 'string', length: 64, nullable: true)]
    private ?string $accountNumber = 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 $terminalNumber,
        ?string $serialNumber = null,
        ?string $accountNumber = null,
    ) {
        $this->uuid           = Uuid::v4()->toRfc4122();
        $this->user           = $user;
        $this->bankName       = $bankName;
        $this->terminalNumber = $terminalNumber;
        $this->serialNumber   = $serialNumber ?: null;
        $this->accountNumber  = $accountNumber ?: 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 getSerialNumber(): ?string   { return $this->serialNumber; }
    public function getTerminalNumber(): string  { return $this->terminalNumber; }
    public function getAccountNumber(): ?string  { return $this->accountNumber; }
    public function isActive(): bool             { return $this->isActive; }

    public function setBankName(string $v): self       { $this->bankName = $v; $this->touch(); return $this; }
    public function setSerialNumber(?string $v): self  { $this->serialNumber = $v ?: null; $this->touch(); return $this; }
    public function setTerminalNumber(string $v): self { $this->terminalNumber = $v; $this->touch(); return $this; }
    public function setAccountNumber(?string $v): self { $this->accountNumber = $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,
            'serial_number'   => $this->serialNumber,
            'terminal_number' => $this->terminalNumber,
            'account_number'  => $this->accountNumber,
            'is_active'       => $this->isActive,
            'created_at'      => $this->createdAt,
            // تهی یعنی «محیطش هنوز تعیین نشده»؛ پنل با همین نشانه‌گذاری‌اش می‌کند.
            'entity_type'     => $this->entityType,
        ];
    }
}
