<?php

namespace App\Patient\Entity;

use App\Auth\Entity\User;
use App\Patient\Repository\PatientNoteRepository;
use App\Shared\Tenant\TenantOwnedTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

/**
 * A personal, pinnable staff note attached to a patient record (the «یادداشت‌ها» tab).
 * Shared across the staff who own the record; the author's display name is denormalised
 * so the note survives the author being deleted.
 */
#[ORM\Entity(repositoryClass: PatientNoteRepository::class)]
#[ORM\Table(name: 'patient_notes')]
#[ORM\Index(columns: ['entity_type', 'entity_id'], name: 'idx_patient_notes_entity')]
#[ORM\Index(columns: ['record_id'], name: 'idx_patient_notes_record')]
class PatientNote
{
    use TenantOwnedTrait;

    #[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: PatientRecord::class)]
    #[ORM\JoinColumn(name: 'record_id', nullable: false, onDelete: 'CASCADE')]
    private PatientRecord $record;

    /** متن یادداشت — free-text body. */
    #[ORM\Column(type: 'text')]
    private string $body;

    /** یادداشت‌های پین‌شده بالای لیست نشان داده می‌شوند. */
    #[ORM\Column(type: 'boolean')]
    private bool $pinned = false;

    /** The staff user who wrote the note; kept for possible audit, nulled if they are removed. */
    #[ORM\ManyToOne(targetEntity: User::class)]
    #[ORM\JoinColumn(name: 'created_by', nullable: true, onDelete: 'SET NULL')]
    private ?User $createdBy = null;

    /** Denormalised author display name (survives user deletion). */
    #[ORM\Column(name: 'author_name', type: 'string', length: 120, nullable: true)]
    private ?string $authorName = null;

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

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

    public function __construct(PatientRecord $record, string $body, bool $pinned = false)
    {
        $this->uuid      = Uuid::v4()->toRfc4122();
        $this->record    = $record;
        $this->assignTenantPair($record->getEntityType(), $record->getEntityId());
        $this->body      = $body;
        $this->pinned    = $pinned;
        $this->createdAt = time();
    }

    public function getId(): ?int              { return $this->id; }
    public function getUuid(): string          { return $this->uuid; }
    public function getRecord(): PatientRecord { return $this->record; }
    public function isPinned(): bool           { return $this->pinned; }

    public function setBody(string $body): self { $this->body = $body; $this->touch(); return $this; }
    public function setPinned(bool $pinned): self { $this->pinned = $pinned; $this->touch(); return $this; }

    public function setAuthor(?User $user, ?string $name): self
    {
        $this->createdBy  = $user;
        $this->authorName = $name;
        return $this;
    }

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

    public function toArray(): array
    {
        return [
            'uuid'       => $this->uuid,
            'body'       => $this->body,
            'pinned'     => $this->pinned,
            'author'     => $this->authorName,
            'created_at' => $this->createdAt,
            'updated_at' => $this->updatedAt,
        ];
    }
}
