<?php

namespace App\Actions;

use App\Enums\RoleSlug;
use App\Models\Role;
use App\Models\User;

class CreateUserAction
{
    /**
     * @param  array{name: string, email: string, password: string, role: RoleSlug}  $data
     */
    public function handle(array $data): User
    {
        $role = Role::query()->where('slug', $data['role'])->firstOrFail();

        return User::query()->create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => $data['password'],
            'role_id' => $role->id,
            'email_verified_at' => now(),
        ]);
    }
}
