<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

/**
 * UpdateKnowledgeBaseAnswerRequest
 * 
 * Validation for updating an existing knowledge base answer.
 * Used in KnowledgeBaseAnswerController@update
 */
class UpdateKnowledgeBaseAnswerRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        if (!auth()->check()) {
            return false;
        }

        $answer = $this->route('answer');

        return auth()->user()->id === $answer->user_id ||
               auth()->user()->hasAnyRole(['Manager', 'Super Manager']) ||
               auth()->user()->can('edit knowledge base answers');
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'content' => 'required|string|min:20|max:10000',
        ];
    }

    /**
     * Get custom validation messages.
     */
    public function messages(): array
    {
        return [
            'content.required' => 'Answer content is required.',
            'content.min' => 'Answer must be at least 20 characters long.',
            'content.max' => 'Answer cannot exceed 10000 characters.',
        ];
    }
}
