<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdatePermissionRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->hasPermission('manage-settings') || $this->user()?->hasRole('admin');
    }

    public function rules(): array
    {
        $permissionId = $this->route('permission')->id ?? null;

        return [
            'name' => ['required', 'string', 'max:255'],
            'slug' => ['required', 'string', 'max:255', 'alpha_dash', Rule::unique('permissions', 'slug')->ignore($permissionId)],
            'description' => ['nullable', 'string', 'max:500'],
            'roles' => ['sometimes', 'array'],
            'roles.*' => ['integer', 'exists:roles,id'],
        ];
    }

    public function messages(): array
    {
        return [
            'name.required' => 'Ime je obvezno.',
            'slug.required' => 'Slug je obvezen.',
            'slug.unique' => 'Slug že obstaja.',
        ];
    }
}
