<?php

namespace App\Billing\Entity;

use App\Billing\Repository\InvoiceItemRepository;
use App\Billing\ValueObject\ShareBreakdown;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: InvoiceItemRepository::class)]
#[ORM\Table(name: 'invoice_items')]
class InvoiceItem
{
    #[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: Invoice::class, inversedBy: 'items')]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private Invoice $invoice;

    #[ORM\Column(name: 'service_item_id', type: 'integer', nullable: true)]
    private ?int $serviceItemId = null;

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

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

    #[ORM\Column(type: 'integer')]
    private int $quantity;

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

    #[ORM\Column(name: 'base_insurance_rials', type: 'integer')]
    private int $baseInsuranceRials = 0;

    #[ORM\Column(name: 'supplementary_rials', type: 'integer')]
    private int $supplementaryRials = 0;

    #[ORM\Column(name: 'patient_rials', type: 'integer')]
    private int $patientRials = 0;

    public function __construct(
        Invoice $invoice,
        string $title,
        int $tariffRials,
        int $quantity,
        ShareBreakdown $breakdown,
        ?int $serviceItemId = null,
    ) {
        $this->uuid               = Uuid::v4()->toRfc4122();
        $this->invoice            = $invoice;
        $this->title              = $title;
        $this->tariffRials        = $tariffRials;
        $this->quantity           = $quantity;
        $this->serviceItemId      = $serviceItemId;
        $this->totalRials         = $breakdown->totalRials;
        $this->baseInsuranceRials = $breakdown->baseInsuranceRials;
        $this->supplementaryRials = $breakdown->supplementaryRials;
        $this->patientRials       = $breakdown->patientRials;
    }

    public function getId(): ?int               { return $this->id; }
    public function getUuid(): string           { return $this->uuid; }
    public function getServiceItemId(): ?int    { return $this->serviceItemId; }
    public function getTotalRials(): int        { return $this->totalRials; }
    public function getBaseInsuranceRials(): int { return $this->baseInsuranceRials; }
    public function getSupplementaryRials(): int { return $this->supplementaryRials; }
    public function getPatientRials(): int      { return $this->patientRials; }

    public function attachTo(Invoice $invoice): void
    {
        $this->invoice = $invoice;
    }

    public function toArray(): array
    {
        return [
            'uuid'                  => $this->uuid,
            'service_item_id'       => $this->serviceItemId,
            'title'                 => $this->title,
            'tariff_rials'          => $this->tariffRials,
            'quantity'              => $this->quantity,
            'total_rials'           => $this->totalRials,
            'base_insurance_rials'  => $this->baseInsuranceRials,
            'supplementary_rials'   => $this->supplementaryRials,
            'patient_rials'         => $this->patientRials,
        ];
    }
}
