<?php

namespace App\Appointment\Entity;

use App\Doctor\Entity\Doctor;
use Doctrine\ORM\Mapping as ORM;
use App\Appointment\Repository\HolidayRepository;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: HolidayRepository::class)]
#[ORM\Table(name: 'holidays')]
#[ORM\Index(columns: ['doctor_id', 'start_date', 'end_date'], name: 'idx_holidays_doctor_range')]
class Holiday
{
    #[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: Doctor::class)]
    #[ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private Doctor $doctor;

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

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

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

    #[ORM\Column(type: 'string', length: 255, nullable: true)]
    private ?string $reason = null;

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

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

    public function __construct(Doctor $doctor, int $startDate, int $endDate)
    {
        $this->uuid      = Uuid::v4()->toRfc4122();
        $this->doctor    = $doctor;
        $this->startDate = $startDate;
        $this->endDate   = $endDate;
        $this->createdAt = time();
        $this->updatedAt = time();
    }

    public function getId(): ?int        { return $this->id; }
    public function getUuid(): string    { return $this->uuid; }
    public function getDoctor(): Doctor  { return $this->doctor; }
    public function getStartDate(): int  { return $this->startDate; }
    public function getEndDate(): int    { return $this->endDate; }
    public function isActive(): bool     { return $this->active; }
    public function getReason(): ?string { return $this->reason; }

    public function setStartDate(int $v): self    { $this->startDate = $v; $this->touch(); return $this; }
    public function setEndDate(int $v): self      { $this->endDate = $v; $this->touch(); return $this; }
    public function setActive(bool $v): self      { $this->active = $v; $this->touch(); return $this; }
    public function setReason(?string $v): self   { $this->reason = $v; $this->touch(); return $this; }

    private function touch(): void { $this->updatedAt = time(); }

    public function toArray(): array
    {
        return [
            'uuid'        => $this->uuid,
            'doctor_uuid' => $this->doctor->getUuid(),
            'start_date'  => $this->startDate,
            'end_date'    => $this->endDate,
            'active'      => $this->active,
            'reason'      => $this->reason,
            'created_at'  => $this->createdAt,
        ];
    }
}
