<?php
namespace App\Entities\Usuarios;

use App\Models\ChatNotification;
use CodeIgniter\Shield\Entities\User;

class UserEntity extends User
{
    protected $attributes = [
        "id" => null,
        "first_name" => null,
        "last_name" => null,
        "cliente_id" => null,
        "status" => null,
        "status_message" => null,
        'active' => null,
        "comments" => null,
        "last_active" => null,
        "created_at" => null,
        "updated_at" => null,
        "deleted_at" => null,
    ];
    protected $casts = [
        "id" => "int",
        "cliente_id" => "int",
        "active" => "boolean",
    ];

    /**
     * Get the full name of the user
     *
     * If the first name and last name are available, the full name is generated as "{first name} {last name}".
     * If the first name or last name is missing, only the available name is used.
     * If both the first name and last name are missing, the username is used as the full name.
     *
     * @return string The full name of the user
     */
    public function getFullName()
    {
        $firstName = trim($this->attributes["first_name"] ?? "");
        $lastName = trim($this->attributes["last_name"] ?? "");
        $fullName = $firstName . ' ' . $lastName;
        $fullName = trim($fullName);  // In case first name is empty, this will remove the leading space

        // Use the username attribute if the full name is still empty after trimming
        return $fullName ?: $this->attributes["username"];
    }

    /**
     * Alias for getFullName()
     *
     * @return string
     */
    public function fullName()
    {
        return $this->getFullName();
    }
    public function withAvatar() : self
    {
        $users = auth()->getProvider();
        $this->attributes["avatar"] = md5($users->findById($this->attributes['id'])->getEmail());
        return $this;
    }
    /**
     * Return an array of ChatNotificationEntities that belongs to the user
     *
     * @return array<ChatNotificationEntity>
     */
    public function chatNotifications()  : array
    {
        $m = model(ChatNotification::class);
        return $m->where('user_id',$this->attributes['id'])->findAll() ?? [];
    }


}
