<?php

namespace App\Patient\Entity;

use App\ClinicService\Entity\ServiceItem;
use App\Patient\Repository\SessionServiceRepository;
use App\Staff\Entity\ClinicStaff;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: SessionServiceRepository::class)]
#[ORM\Table(name: 'session_services')]
class SessionService
{
    #[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: PatientSession::class, inversedBy: 'services')]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private PatientSession $session;

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

    #[ORM\ManyToOne(targetEntity: ClinicStaff::class)]
    #[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
    private ?ClinicStaff $staff = null;

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

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

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

    public function __construct(PatientSession $session, ServiceItem $serviceItem, ?ClinicStaff $staff = null, int $quantity = 1)
    {
        $this->uuid        = Uuid::v4()->toRfc4122();
        $this->session     = $session;
        $this->serviceItem = $serviceItem;
        $this->staff       = $staff;
        $this->priceRials  = $serviceItem->getPriceRials();
        $this->quantity    = max(1, $quantity);
        $this->createdAt   = time();
    }

    public function getId(): ?int              { return $this->id; }
    public function getUuid(): string          { return $this->uuid; }
    public function getSession(): PatientSession { return $this->session; }
    public function getServiceItem(): ServiceItem { return $this->serviceItem; }
    public function getStaff(): ?ClinicStaff   { return $this->staff; }
    public function getPriceRials(): int        { return $this->priceRials; }
    public function getQuantity(): int          { return $this->quantity; }
    public function getLineTotalRials(): int    { return $this->priceRials * $this->quantity; }
    public function getCreatedAt(): int         { return $this->createdAt; }

    public function toArray(): array
    {
        return [
            'uuid'              => $this->uuid,
            'service_item_uuid' => $this->serviceItem->getUuid(),
            'service_name'      => $this->serviceItem->getName(),
            'staff_uuid'        => $this->staff?->getUuid(),
            'staff_name'        => $this->staff?->getFullName(),
            'price_rials'       => $this->priceRials,
            'quantity'          => $this->quantity,
            'line_total_rials'  => $this->getLineTotalRials(),
            'created_at'        => $this->createdAt,
        ];
    }
}
