<?php

namespace App\Specialty\Entity;

use App\Specialty\Repository\SpecialtyRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: SpecialtyRepository::class)]
#[ORM\Table(name: 'specialties')]
#[ORM\UniqueConstraint(name: 'uq_specialties_slug', columns: ['slug'])]
#[ORM\Index(columns: ['status'], name: 'idx_specialties_status')]
#[ORM\Index(columns: ['parent_id'], name: 'idx_specialties_parent')]
class Specialty
{
    #[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(type: 'string', length: 255)]
    private string $name;

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

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

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

    #[ORM\ManyToOne(targetEntity: self::class)]
    #[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private ?self $parent = null;

    public function __construct(string $name, string $slug, ?self $parent = null)
    {
        $this->uuid   = Uuid::v4()->toRfc4122();
        $this->name   = $name;
        $this->slug   = $slug;
        $this->parent = $parent;
    }

    public function getId(): ?int      { return $this->id; }
    public function getUuid(): string  { return $this->uuid; }
    public function getName(): string  { return $this->name; }
    public function getSlug(): string  { return $this->slug; }
    public function getStatus(): int   { return $this->status; }
    public function getWeight(): int   { return $this->weight; }
    public function getParent(): ?self { return $this->parent; }

    public function setName(string $v): self    { $this->name = $v; return $this; }
    public function setSlug(string $v): self    { $this->slug = $v; return $this; }
    public function setStatus(int $v): self     { $this->status = $v; return $this; }
    public function setWeight(int $v): self     { $this->weight = $v; return $this; }
    public function setParent(?self $v): self   { $this->parent = $v; return $this; }

    public function toArray(): array
    {
        return [
            'id'        => $this->id,
            'uuid'      => $this->uuid,
            'name'      => $this->name,
            'slug'      => $this->slug,
            'status'    => $this->status,
            'weight'    => $this->weight,
            'parent_id' => $this->parent?->getId(),
        ];
    }
}
