<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;
use Spatie\Permission\Models\Role;

class CreateAdminUserRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'name'  => ['required', 'string', 'max:255'],
            'email' => ['required', 'email', 'unique:users,email'],
            'roles' => ['required', 'array', 'min:1'],
            'roles.*' => [
                'string',
                'exists:roles,name',
                function (string $attribute, mixed $value, \Closure $fail) {
                    $role = Role::where('name', $value)->where('guard_name', 'web')->first();
                    if (! $role) {
                        $fail("The role \"{$value}\" does not exist.");
                    }
                },
            ],
        ];
    }
}
