<?php

namespace App\Resource\Entity;

use App\Resource\Repository\ResourcePoolMemberRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * عضویت یک منبع در یک استخر. فرزند aggregate با ریشهٔ {@see ResourcePool} که خودش
 * جفت محیط دارد؛ uuid ندارد و فقط از `PUT /resource-pool/{uuid}/members` نوشته می‌شود.
 */
#[ORM\Entity(repositoryClass: ResourcePoolMemberRepository::class)]
#[ORM\Table(name: 'resource_pool_members')]
#[ORM\UniqueConstraint(name: 'uniq_pool_resource', columns: ['pool_id', 'resource_id'])]
#[ORM\Index(columns: ['resource_id'], name: 'idx_pool_members_resource')]
class ResourcePoolMember
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: ResourcePool::class, inversedBy: 'members')]
    #[ORM\JoinColumn(name: 'pool_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private ResourcePool $pool;

    #[ORM\ManyToOne(targetEntity: ClinicResource::class)]
    #[ORM\JoinColumn(name: 'resource_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private ClinicResource $resource;

    /** ترتیب ترجیح در استراتژی انتخابِ تسک ۰۶؛ کوچک‌تر = زودتر. */
    #[ORM\Column(type: 'smallint', options: ['default' => 0])]
    private int $priority = 0;

    public function __construct(ResourcePool $pool, ClinicResource $resource, int $priority = 0)
    {
        $this->pool     = $pool;
        $this->resource = $resource;
        $this->priority = $priority;
    }

    public function getId(): ?int                  { return $this->id; }
    public function getPool(): ResourcePool        { return $this->pool; }
    public function getResource(): ClinicResource  { return $this->resource; }
    public function getPriority(): int             { return $this->priority; }

    public function toArray(): array
    {
        return [
            'resource_uuid' => $this->resource->getUuid(),
            'resource_name' => $this->resource->getName(),
            'priority'      => $this->priority,
            'active'        => $this->resource->isActive(),
        ];
    }
}
