<?php

namespace App\Filament\Resources\Users\Schemas;

use App\Support\Roles;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Support\Facades\Hash;

class UserForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Identité')
                    ->columns(2)
                    ->schema([
                        TextInput::make('name')
                            ->label('Nom complet')
                            ->required()
                            ->maxLength(255),
                        TextInput::make('email')
                            ->label('E-mail')
                            ->email()
                            ->required()
                            ->unique(ignoreRecord: true),
                        TextInput::make('phone')
                            ->label('Téléphone')
                            ->tel(),
                        Select::make('roles')
                            ->label('Rôle')
                            ->relationship('roles', 'name')
                            ->options(Roles::labels())
                            ->required()
                            ->preload(),
                    ]),
                Section::make('Accès & sécurité')
                    ->columns(2)
                    ->schema([
                        TextInput::make('password')
                            ->label('Mot de passe')
                            ->password()
                            ->revealable()
                            ->dehydrateStateUsing(fn ($state) => Hash::make($state))
                            ->dehydrated(fn ($state) => filled($state))
                            ->required(fn (string $operation) => $operation === 'create')
                            ->helperText('Laisser vide pour conserver le mot de passe actuel.'),
                        Toggle::make('must_change_password')
                            ->label('Imposer le changement à la prochaine connexion')
                            ->default(true),
                        Toggle::make('is_active')
                            ->label('Compte actif')
                            ->default(true),
                    ]),
            ]);
    }
}
