<?php

namespace App\Models;

use Database\Factories\UserFactory;
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasName;
use Filament\Panel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

/**
 * Compte administrateur (auth Filament) — table `utilisateur`, préfixe `usr` (convention projet).
 */
class User extends Authenticatable implements FilamentUser, HasName
{
    /** @use HasFactory<UserFactory> */
    use HasFactory, Notifiable;

    protected $table = 'utilisateur';

    protected $primaryKey = 'usr_id';

    const CREATED_AT = 'usr_created_at';

    const UPDATED_AT = 'usr_updated_at';

    protected $fillable = [
        'usr_nom',
        'usr_email',
        'usr_password',
    ];

    protected $hidden = [
        'usr_password',
        'remember_token',
    ];

    protected function casts(): array
    {
        return [
            'usr_email_verified_at' => 'datetime',
            'usr_password' => 'hashed',
        ];
    }

    // ── Adaptation de l'auth Laravel aux colonnes préfixées ─────────
    public function getAuthPassword()
    {
        return $this->usr_password;
    }

    public function getAuthPasswordName()
    {
        return 'usr_password';
    }

    public function getEmailForPasswordReset()
    {
        return $this->usr_email;
    }

    /** Filament affiche ce champ comme nom de l'utilisateur connecté. */
    public function getFilamentName(): string
    {
        return $this->usr_nom;
    }

    /** Tout compte de la table `utilisateur` est un administrateur autorisé. */
    public function canAccessPanel(Panel $panel): bool
    {
        return true;
    }
}
