<?php

namespace App\ClinicService\Entity;

use App\ClinicService\Repository\TariffRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: TariffRepository::class)]
#[ORM\Table(name: 'service_tariffs')]
#[ORM\UniqueConstraint(name: 'uniq_service_tariff_year', columns: ['service_item_id', 'year'])]
#[ORM\Index(columns: ['service_item_id', 'is_active'], name: 'idx_tariff_service_active')]
class Tariff
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 36, unique: true)]
    private string $uuid;

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

    #[ORM\Column(type: 'smallint')]
    private int $year;

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

    #[ORM\Column(name: 'is_active', type: 'boolean')]
    private bool $isActive = true;

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

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

    public function __construct(int $serviceItemId, int $year, int $priceRials = 0)
    {
        $this->uuid          = Uuid::v4()->toRfc4122();
        $this->serviceItemId = $serviceItemId;
        $this->year          = $year;
        $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 getServiceItemId(): int { return $this->serviceItemId; }
    public function getYear(): int          { return $this->year; }
    public function getPriceRials(): int    { return $this->priceRials; }
    public function isActive(): bool        { return $this->isActive; }

    public function setPriceRials(int $v): self { $this->priceRials = $v; $this->updatedAt = time(); return $this; }
    public function setActive(bool $v): self    { $this->isActive = $v; $this->updatedAt = time(); return $this; }

    public function toArray(): array
    {
        return [
            'uuid'            => $this->uuid,
            'service_item_id' => $this->serviceItemId,
            'year'            => $this->year,
            'price_rials'     => $this->priceRials,
            'is_active'       => $this->isActive,
        ];
    }
}
