<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable {
    use Notifiable;
    protected $guarded = ['id'];
    protected $hidden = ['password', 'remember_token'];
    protected function casts(): array {
        return ['email_verified_at' => 'datetime', 'password' => 'hashed', 'is_active' => 'boolean', 'last_login_at' => 'datetime'];
    }
    public function roles() { return $this->belongsToMany(Role::class); }
    public function employee() { return $this->hasOne(Employee::class); }
    public function isSuperAdmin(): bool { return $this->roles()->where('slug', 'super-admin')->exists(); }
    public function permList(): array {
        return $this->roles()->with('permissions')->get()->pluck('permissions.*.name')->flatten()->unique()->values()->all();
    }
    public function canDo(string $perm): bool {
        if ($this->isSuperAdmin()) return true;
        return in_array($perm, $this->permList(), true);
    }
    public function getDisplayNameAttribute(): string { return (string) $this->name; }
}
