<?php

namespace App\Tag\Entity;

use App\Tag\Repository\TenantTagRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

/**
 * A per-tenant (doctor/clinic) label with a display color. Distinct from the
 * global slug-based {@see Tag} taxonomy — these are owned and managed by each
 * tenant for their own use (e.g. patient/appointment labelling).
 */
#[ORM\Entity(repositoryClass: TenantTagRepository::class)]
#[ORM\Table(name: 'tenant_tags')]
#[ORM\Index(columns: ['entity_type', 'entity_id'], name: 'idx_tenant_tags_owner')]
class TenantTag
{
    #[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: 60)]
    private string $name;

    /** Hex color, e.g. "#5559CE". */
    #[ORM\Column(type: 'string', length: 9)]
    private string $color = '#5559CE';

    #[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;

    public function __construct(string $entityType, int $entityId, string $name, string $color = '#5559CE')
    {
        $this->uuid       = Uuid::v4()->toRfc4122();
        $this->entityType = $entityType;
        $this->entityId   = $entityId;
        $this->name       = $name;
        $this->color      = $color;
        $this->createdAt  = time();
        $this->updatedAt  = time();
    }

    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 getColor(): string      { return $this->color; }
    public function isActive(): bool        { return $this->active; }

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

    public function toArray(): array
    {
        return [
            'uuid'   => $this->uuid,
            'name'   => $this->name,
            'color'  => $this->color,
            'active' => $this->active,
        ];
    }
}
