<?php

namespace App\Resource\Entity;

use App\Resource\Repository\ResourceTypeRepository;
use App\Shared\Tenant\TenantOwnedTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

/**
 * نوع منبع — کلینیک خودش تعریفش می‌کند: پزشک، اپراتور، اتاق، دستگاه لیزر، یونیت.
 *
 * سه نوعِ `doctor`/`staff`/`room` را backfill می‌سازد و `isSystem` علامتشان می‌زند،
 * چون `ResourceLinker` با همین کدها پل می‌زند؛ حذفشان یعنی شکستن آن نگاشت.
 */
#[ORM\Entity(repositoryClass: ResourceTypeRepository::class)]
#[ORM\Table(name: 'resource_types')]
#[ORM\Index(columns: ['entity_type', 'entity_id', 'active'], name: 'idx_resource_types_tenant')]
#[ORM\UniqueConstraint(name: 'uniq_rt_tenant_code', columns: ['entity_type', 'entity_id', 'code'])]
class ResourceType
{
    use TenantOwnedTrait;

    public const CODE_DOCTOR = 'doctor';
    public const CODE_STAFF  = 'staff';
    public const CODE_ROOM   = 'room';

    /** کدهایی که ResourceLinker به آن‌ها تکیه دارد و backfill می‌سازدشان. */
    public const SYSTEM_CODES = [
        self::CODE_DOCTOR => 'پزشک',
        self::CODE_STAFF  => 'پرسنل',
        self::CODE_ROOM   => 'اتاق',
    ];

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

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

    #[ORM\Column(type: 'string', length: 40)]
    private string $code;

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

    #[ORM\Column(name: 'is_system', type: 'boolean', options: ['default' => false])]
    private bool $isSystem = false;

    #[ORM\Column(type: 'boolean', options: ['default' => true])]
    private bool $active = true;

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

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

    public function __construct(string $entityType, int $entityId, string $code, string $name)
    {
        $this->uuid      = Uuid::v4()->toRfc4122();
        $this->code      = $code;
        $this->name      = $name;
        $this->createdAt = time();
        $this->updatedAt = time();

        $this->assignTenantPair($entityType, $entityId);
    }

    public function getId(): ?int        { return $this->id; }
    public function getUuid(): string    { return $this->uuid; }
    public function getCode(): string    { return $this->code; }
    public function getName(): string    { return $this->name; }
    public function isSystem(): bool     { return $this->isSystem; }
    public function isActive(): bool     { return $this->active; }
    public function getCreatedAt(): int  { return $this->createdAt; }
    public function getUpdatedAt(): int  { return $this->updatedAt; }

    public function markSystem(): self        { $this->isSystem = true; return $this; }
    public function setName(string $v): self  { $this->name = $v; $this->touch(); return $this; }
    public function setActive(bool $v): self  { $this->active = $v; $this->touch(); return $this; }

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

    public function toArray(?int $resourcesCount = null): array
    {
        $row = [
            'uuid'       => $this->uuid,
            'code'       => $this->code,
            'name'       => $this->name,
            'is_system'  => $this->isSystem,
            'active'     => $this->active,
            'created_at' => $this->createdAt,
            'updated_at' => $this->updatedAt,
        ];

        if ($resourcesCount !== null) {
            $row['resources_count'] = $resourcesCount;
        }

        return $row;
    }
}
