<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Usuario extends Authenticatable
{
    use Notifiable;

    protected $table = 'usuarios';

    protected $primaryKey = 'id_usuario';

    const CREATED_AT = 'fecha_creacion';

    const UPDATED_AT = 'fecha_actualizacion';

    protected $fillable = [
        'nombres',
        'apellidos',
        'correo',
        'password',
        'rol',
        'id_rol',
        'estado',
        'ultimo_acceso',
        'email_verificado_en',
    ];

    protected $hidden = [
        'password',
    ];

    protected $casts = [
        'ultimo_acceso' => 'datetime',
        'email_verificado_en' => 'datetime',
    ];

    public function routeNotificationForMail(mixed $notification = null): ?string
    {
        $correo = is_string($this->correo ?? null) ? trim((string) $this->correo) : '';

        return $correo !== '' ? $correo : null;
    }

    public function getRouteKeyName(): string
    {
        return $this->getKeyName();
    }

    public function historial(): HasMany
    {
        return $this->hasMany(Historial::class, 'id_usuario', 'id_usuario');
    }

    public function apiTokens(): HasMany
    {
        return $this->hasMany(ApiToken::class, 'id_usuario', 'id_usuario');
    }

    public function rolDetalle(): BelongsTo
    {
        return $this->belongsTo(Rol::class, 'id_rol', 'id_rol');
    }
}
