<?php
namespace Incoviba\Model;

use Incoviba\Common\Ideal;
use Incoviba\Model\Persona\Datos;

class Persona extends Ideal\Model
{
    public int $rut;
    public string $digito;
    public string $nombres;
    public string $apellidoPaterno;
    public string $apellidoMaterno;

    public function nombreCompleto(): string
    {
        return $this->nombres . ' ' . $this->apellidoPaterno . ' ' . $this->apellidoMaterno;
    }

    public function rutCompleto(): string
    {
        return number_format($this->rut, 0, ',', '.') . '-' . $this->digito;
    }

    protected ?Datos $datos;
    public function datos(): ?Datos
    {
        if (!isset($this->datos)) {
            $this->datos = $this->runFactory('datos');
        }
        return $this->datos;
    }

    public function jsonSerialize(): mixed
    {
        return [
            'rut' => $this->rut,
            'digito' => $this->digito,
            'nombres' => $this->nombres,
            'apellidoPaterno' => $this->apellidoPaterno,
            'apellidoMaterno' => $this->apellidoMaterno,
            'nombreCompleto' => $this->nombreCompleto(),
            'rutCompleto' => $this->rutCompleto(),
            'datos' => $this->datos() ?? null,
        ];
    }
}
