<?php

namespace Functional\Users\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Functional\Users\Database\Factories\UserFactory;
use Functional\Users\Enums\Units;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Attributes\UseFactory;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use NotificationChannels\WebPush\HasPushSubscriptions;
use Spatie\Permission\Traits\HasRoles;

/**
 * @property string $locale
 * @property Units $units
 * @property string|null $timezone
 * @property string|null $avatar_path
 * @property string|null $oidc_sub
 * @property array<int, string>|null $available_equipment
 */
#[Fillable(['name', 'email', 'password', 'locale', 'units', 'timezone', 'avatar_path', 'oidc_sub', 'available_equipment'])]
#[Hidden(['password', 'remember_token'])]
#[UseFactory(UserFactory::class)]
class User extends Authenticatable implements FilamentUser
{
    use HasApiTokens, HasFactory, HasPushSubscriptions, HasRoles, Notifiable;

    /** Accès au back-office Filament : réservé aux rôles "super_admin" ou "admin" (Filament Shield). */
    public function canAccessPanel(Panel $panel): bool
    {
        return $this->hasAnyRole(['super_admin', 'admin']);
    }

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
            'units' => Units::class,
            'available_equipment' => 'array',
        ];
    }
}
