<?php

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeUserMail;
use App\Models\User;


class CreateUser extends CreateRecord
{
    protected static string $resource = UserResource::class;
    protected function handleRecordCreation(array $data): User
    {
       
        // Crear el usuario con la contraseña hasheada
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => $data['password'],
        ]);

        // Asignar roles si es necesario
        if (isset($data['roles'])) {
            $user->roles()->sync($data['roles']);
        }
        $password=$data['password'];

        // Enviar el correo de bienvenida
        Mail::to($user->email)->send(new WelcomeUserMail($user, $password));

        return $user;
    }
}
