<?php

namespace App\ClinicService\Entity;

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

#[ORM\Entity(repositoryClass: ServiceItemRepository::class)]
#[ORM\Table(name: 'service_items')]
class ServiceItem
{
    #[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: ServiceSection::class, inversedBy: 'items')]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private ServiceSection $section;

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

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

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

    #[ORM\Column(type: 'boolean')]
    private bool $active = true;

    #[ORM\Column(name: 'insurance_covered', type: 'boolean')]
    private bool $insuranceCovered = false;

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

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

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

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

    public function __construct(ServiceSection $section, string $name, int $priceRials = 0)
    {
        $this->uuid       = Uuid::v4()->toRfc4122();
        $this->section    = $section;
        $this->name       = $name;
        $this->priceRials = $priceRials;
        $this->createdAt  = time();
        $this->updatedAt  = time();
    }

    public function getId(): ?int              { return $this->id; }
    public function getUuid(): string          { return $this->uuid; }
    public function getSection(): ServiceSection { return $this->section; }
    public function getStaff(): ?ClinicStaff   { return $this->staff; }
    public function getName(): string          { return $this->name; }
    public function getPriceRials(): int       { return $this->priceRials; }
    public function isActive(): bool           { return $this->active; }
    public function isInsuranceCovered(): bool  { return $this->insuranceCovered; }
    public function getInsurancePriceRials(): ?int { return $this->insurancePriceRials; }
    public function getDurationMinutes(): ?int { return $this->durationMinutes; }
    public function getCreatedAt(): int        { return $this->createdAt; }
    public function getUpdatedAt(): int        { return $this->updatedAt; }

    public function setStaff(?ClinicStaff $staff): self  { $this->staff = $staff; $this->updatedAt = time(); return $this; }
    public function setName(string $name): self           { $this->name = $name; $this->updatedAt = time(); return $this; }
    public function setPriceRials(int $price): self       { $this->priceRials = $price; $this->updatedAt = time(); return $this; }
    public function setActive(bool $active): self         { $this->active = $active; $this->updatedAt = time(); return $this; }
    public function setInsuranceCovered(bool $v): self    { $this->insuranceCovered = $v; $this->updatedAt = time(); return $this; }
    public function setInsurancePriceRials(?int $v): self { $this->insurancePriceRials = $v; $this->updatedAt = time(); return $this; }
    public function setDurationMinutes(?int $v): self { $this->durationMinutes = $v; $this->updatedAt = time(); return $this; }

    public function toArray(): array
    {
        return [
            'uuid'         => $this->uuid,
            'section_uuid' => $this->section->getUuid(),
            'staff_uuid'   => $this->staff?->getUuid(),
            'staff_name'   => $this->staff?->getFullName(),
            'staff'        => $this->staff !== null
                ? ['uuid' => $this->staff->getUuid(), 'full_name' => $this->staff->getFullName()]
                : null,
            'name'         => $this->name,
            'price_rials'  => $this->priceRials,
            'active'       => $this->active,
            'insurance_covered'     => $this->insuranceCovered,
            'insurance_price_rials' => $this->insurancePriceRials,
            'duration_minutes'      => $this->durationMinutes,
            'created_at'   => $this->createdAt,
            'updated_at'   => $this->updatedAt,
        ];
    }
}
