<?php

namespace App\Http\Requests\Faq;

use Illuminate\Foundation\Http\FormRequest;

class FaqCreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        if (auth()->user()->hasRole('administrator')) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'product_id' => 'nullable',
            'faqs' => 'required|array|min:1',
            'faqs.*.question' => 'required|string',
            'faqs.*.answer' => 'required|string',
        ];
    }

    /**
     * Get custom validation messages.
     *
     * @return array
     */
    public function messages(): array
    {
        return [
            'faqs.required' => 'Please add at least one FAQ.',
            'faqs.array' => 'The FAQ list must be an array.',
            'faqs.min' => 'Please add at least one question to the FAQ list.',
            'faqs.*.question.required' => 'Each FAQ must have a question.',
            'faqs.*.answer.required' => 'Each FAQ must have an answer.',
        ];
    }
}
