<?php

namespace App\Patient\Entity;

use App\Patient\Repository\SessionAuditLogRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

/**
 * تاریخچه‌ی تغییرات مالی/خدماتی یک مراجعه (Audit Log): چه کسی، چه چیزی، کِی و
 * مقدار قبل/بعد را تغییر داد. برای شفافیت و قابلیت پیگیری کامل پرونده.
 */
#[ORM\Entity(repositoryClass: SessionAuditLogRepository::class)]
#[ORM\Table(name: 'session_audit_logs')]
#[ORM\Index(columns: ['session_id', 'created_at'], name: 'idx_session_audit_session')]
class SessionAuditLog
{
    public const OP_CREATE = 'create';
    public const OP_UPDATE = 'update';
    public const OP_DELETE = 'delete';

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

    /** فیلد تغییرکرده: visit_price_rials | services | consumables | payment | discount | ... */
    #[ORM\Column(type: 'string', length: 40)]
    private string $field;

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

    #[ORM\Column(name: 'old_value', type: 'text', nullable: true)]
    private ?string $oldValue = null;

    #[ORM\Column(name: 'new_value', type: 'text', nullable: true)]
    private ?string $newValue = null;

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

    #[ORM\Column(name: 'actor_name', type: 'string', length: 191, nullable: true)]
    private ?string $actorName = null;

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

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

    public function __construct(PatientSession $session, string $field, string $operation)
    {
        $this->uuid      = Uuid::v4()->toRfc4122();
        $this->session   = $session;
        $this->field     = $field;
        $this->operation = $operation;
        $this->createdAt = time();
    }

    public function setActor(?int $userId, ?string $name): self { $this->actorUserId = $userId; $this->actorName = $name; return $this; }
    public function setValues(?string $old, ?string $new): self { $this->oldValue = $old; $this->newValue = $new; return $this; }
    public function setNote(?string $note): self { $this->note = $note; return $this; }

    public function getUuid(): string { return $this->uuid; }

    public function toArray(): array
    {
        return [
            'uuid'       => $this->uuid,
            'field'      => $this->field,
            'operation'  => $this->operation,
            'old_value'  => $this->oldValue,
            'new_value'  => $this->newValue,
            'actor_name' => $this->actorName,
            'note'       => $this->note,
            'created_at' => $this->createdAt,
        ];
    }
}
