<?php

namespace App\Http\Requests\Authz;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            'role_names' => 'sometimes|array',
            'role_names.*' => 'string|min:1',
            'role_ids' => 'sometimes|array',
            'role_ids.*' => 'integer|min:1',
        ];
    }

    public function withValidator($validator): void
    {
        $validator->after(function ($validator) {
            $data = $this->all();
            if (empty($data['role_names']) && empty($data['role_ids'])) {
                $validator->errors()->add('role_names', __('error.role.role_input_required'));
            }
        });
    }
}
