<?php

namespace App\Http\Requests\Member;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            'title_en'                => ['required', 'string', 'max:255'],
            'title_ar'                => ['nullable', 'string', 'max:255'],
            'description_en'          => ['nullable', 'string', 'max:2000'],
            'description_ar'          => ['nullable', 'string', 'max:2000'],
            'audience'                => ['required', 'in:all,hub,custom'],
            'hub_id'                  => ['nullable', 'required_if:audience,hub', 'exists:hubs,id'],
            'anonymous_responses'     => ['boolean'],
            'show_results_to_members' => ['boolean'],
            'closes_at'               => ['nullable', 'date', 'after:today'],

            // Questions array
            'questions'                    => ['nullable', 'array', 'max:50'],
            'questions.*.question_en'      => ['required', 'string', 'max:1000'],
            'questions.*.question_ar'      => ['nullable', 'string', 'max:1000'],
            'questions.*.type'             => ['required', 'in:multiple_choice,multi_select,rating,text,yes_no'],
            'questions.*.options_en'       => ['nullable', 'array'],
            'questions.*.options_en.*'     => ['nullable', 'string', 'max:255'],
            'questions.*.options_ar'       => ['nullable', 'array'],
            'questions.*.options_ar.*'     => ['nullable', 'string', 'max:255'],
            'questions.*.rating_min'       => ['nullable', 'integer', 'min:0', 'max:10'],
            'questions.*.rating_max'       => ['nullable', 'integer', 'min:1', 'max:10'],
            'questions.*.is_required'      => ['boolean'],
            'questions.*.bank_item_id'     => ['nullable', 'exists:question_bank_items,id'],
        ];
    }

    public function messages(): array
    {
        return [
            'title_en.required'           => __('survey.validation_title_required'),
            'hub_id.required_if'          => __('survey.validation_hub_required'),
            'closes_at.after'             => __('survey.validation_closes_at_future'),
            'questions.*.question_en.required' => __('survey.validation_question_text_required'),
            'questions.*.type.required'   => __('survey.validation_question_type_required'),
            'questions.*.type.in'         => __('survey.validation_question_type_invalid'),
        ];
    }
}
