<?php

namespace App\ClinicService\Entity;

use App\ClinicService\Repository\ServiceSectionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: ServiceSectionRepository::class)]
#[ORM\Table(name: 'service_sections')]
#[ORM\Index(columns: ['entity_type', 'entity_id'], name: 'idx_service_section_entity')]
class ServiceSection
{
    #[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: 'entity_type', type: 'string', length: 10)]
    private string $entityType;

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

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

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

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

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

    #[ORM\OneToMany(targetEntity: ServiceItem::class, mappedBy: 'section', cascade: ['remove'])]
    private Collection $items;

    public function __construct(string $entityType, int $entityId, string $name)
    {
        $this->uuid       = Uuid::v4()->toRfc4122();
        $this->entityType = $entityType;
        $this->entityId   = $entityId;
        $this->name       = $name;
        $this->createdAt  = time();
        $this->updatedAt  = time();
        $this->items      = new ArrayCollection();
    }

    public function getId(): ?int          { return $this->id; }
    public function getUuid(): string      { return $this->uuid; }
    public function getEntityType(): string { return $this->entityType; }
    public function getEntityId(): int     { return $this->entityId; }
    public function getName(): string      { return $this->name; }
    public function isActive(): bool       { return $this->active; }
    public function getCreatedAt(): int    { return $this->createdAt; }
    public function getUpdatedAt(): int    { return $this->updatedAt; }

    public function setName(string $name): self   { $this->name = $name; $this->updatedAt = time(); return $this; }
    public function setActive(bool $active): self { $this->active = $active; $this->updatedAt = time(); return $this; }

    public function toArray(): array
    {
        return [
            'uuid'        => $this->uuid,
            'entity_type' => $this->entityType,
            'entity_id'   => $this->entityId,
            'name'        => $this->name,
            'active'      => $this->active,
            'created_at'  => $this->createdAt,
            'updated_at'  => $this->updatedAt,
        ];
    }
}
