<?php

namespace App\ClinicService\Entity;

use App\ClinicService\Repository\ItemGroupMemberRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * عضویت یک آیتم در یک گروه انتخاب. فرزند aggregate با ریشهٔ {@see ItemGroup} که خودش
 * جفت محیط دارد؛ uuid ندارد و فقط از `PUT /item-group/{uuid}/items` نوشته می‌شود.
 */
#[ORM\Entity(repositoryClass: ItemGroupMemberRepository::class)]
#[ORM\Table(name: 'service_item_group_members')]
#[ORM\UniqueConstraint(name: 'uniq_group_item', columns: ['group_id', 'item_id'])]
#[ORM\Index(columns: ['item_id'], name: 'idx_group_member_item')]
class ItemGroupMember
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

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

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

    #[ORM\Column(name: 'sort_order', type: 'smallint', options: ['default' => 0])]
    private int $sortOrder = 0;

    public function __construct(ItemGroup $group, ServiceItem $item, int $sortOrder = 0)
    {
        $this->group     = $group;
        $this->item      = $item;
        $this->sortOrder = $sortOrder;
    }

    public function getId(): ?int          { return $this->id; }
    public function getGroup(): ItemGroup  { return $this->group; }
    public function getItem(): ServiceItem { return $this->item; }
    public function getSortOrder(): int    { return $this->sortOrder; }

    public function toArray(): array
    {
        return [
            'item_uuid'                  => $this->item->getUuid(),
            'item_name'                  => $this->item->getName(),
            'solo_duration_minutes'      => $this->item->getSoloDurationMinutes(),
            'additional_duration_minutes' => $this->item->effectiveAdditionalMinutes(),
            'price_rials'                => $this->item->getPriceRials(),
            'sort_order'                 => $this->sortOrder,
            'active'                     => $this->item->isActive(),
        ];
    }
}
