<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $table = 'usuarios';
    protected $primaryKey = 'id_usuario';
    public $timestamps = false;
    
    /**
     * El campo que se usa para autenticación (usuario en lugar de email)
     */
    public function username()
    {
        return 'usuario';
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'nombres',
        'apellidos',
        'usuario',
        'clave',
        'rol',
        'estado',
        'must_change_password',
    ];

    /**
     * Accessor para obtener el nombre completo
     */
    public function getNameAttribute()
    {
        return trim($this->nombres . ' ' . ($this->apellidos ?? ''));
    }

    /**
     * Mutator para establecer nombres desde name
     */
    public function setNameAttribute($value)
    {
        $this->attributes['nombres'] = $value;
    }

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'clave',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'estado' => 'boolean',
        'clave' => 'hashed',
        'fecha_creacion' => 'datetime',
    ];

    /**
     * Mapear el atributo 'password' a la columna 'clave' para compatibilidad con Laravel
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['clave'] = $value;
    }

    public function getAuthPassword()
    {
        return $this->clave;
    }

    public function getRememberTokenName()
    {
        return null;
    }

    public function getRememberToken()
    {
        return null;
    }

    public function setRememberToken($value)
    {
        // No remember token column in usuarios table
    }

    public function registrosSalidas()
    {
        return $this->hasMany(\App\Models\RegistroSalida::class, 'id_usuario', 'id_usuario');
    }
}
