<?php

namespace App\Pricing\Entity;

use App\ClinicService\Entity\ServiceItem;
use App\Pricing\Repository\PriceListItemRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * قیمت یک سرویس در یک لیست قیمت. فرزند aggregate با ریشهٔ {@see PriceList} که خودش
 * جفت محیط دارد؛ uuid از request نمی‌گیرد.
 */
#[ORM\Entity(repositoryClass: PriceListItemRepository::class)]
#[ORM\Table(name: 'price_list_items')]
#[ORM\UniqueConstraint(name: 'uniq_price_list_service', columns: ['price_list_id', 'service_item_id'])]
class PriceListItem
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: PriceList::class, inversedBy: 'items')]
    #[ORM\JoinColumn(name: 'price_list_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private PriceList $priceList;

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

    #[ORM\Column(name: 'price_rials', type: 'bigint')]
    private int $priceRials;

    public function __construct(PriceList $priceList, ServiceItem $serviceItem, int $priceRials)
    {
        if ($priceRials < 0) {
            throw new \InvalidArgumentException('Price cannot be negative.');
        }

        $this->priceList   = $priceList;
        $this->serviceItem = $serviceItem;
        $this->priceRials  = $priceRials;
    }

    public function getId(): ?int                 { return $this->id; }
    public function getPriceList(): PriceList     { return $this->priceList; }
    public function getServiceItem(): ServiceItem { return $this->serviceItem; }
    public function getPriceRials(): int          { return (int) $this->priceRials; }

    public function toArray(): array
    {
        return [
            'service_uuid' => $this->serviceItem->getUuid(),
            'service_name' => $this->serviceItem->getName(),
            'price_rials'  => $this->getPriceRials(),
        ];
    }
}
