<?php

namespace App\Http\Requests\Admin\RoleManagement;

use Illuminate\Foundation\Http\FormRequest;

class RoleManagementStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->user()->can('create', \Spatie\Permission\Models\Role::class);
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'name' => 'required|string|max:255|unique:roles,name',
            'guard_name' => 'nullable|string|in:admin,web',
            'permissions' => 'nullable|array',
            'permissions.*' => 'string|exists:permissions,name',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'name.required' => __('admin/validation.role_management_name_required'),
            'name.unique' => __('admin/validation.role_management_name_unique'),
            'permissions.*.exists' => __('admin/validation.role_management_permissions_exists'),
        ];
    }
}
