<?php

namespace App\Http\Requests\Admin;

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

class UpdateQuestionRequest extends FormRequest
{
    public function authorize(): bool
    {
        return auth()->check();
    }

    public function rules(): array
    {
        return [
            'editid' => ['required', 'integer', 'exists:questionsmanagemnet,id'],
            'questiontitle' => ['required', 'string', 'max:2000', Rule::unique('questionsmanagemnet', 'questionstitle')->ignore($this->integer('editid'))],
            'questionsubject' => ['required', 'string', 'max:255'],
            'questiongoupsclass' => ['required', 'string', 'max:255'],
            'questionchapter' => ['nullable', 'string', 'max:250'],
            'questionsection' => ['required', 'array', 'size:10'],
            'questionsection.0' => ['required', 'string', 'max:3000'],
            'questionsection.*' => ['nullable', 'string', 'max:3000'],
            'questionsectionhndi' => ['required', 'array', 'size:10'],
            'questionsectionhndi.*' => ['nullable', 'string', 'max:3000'],
            'questionsectionobjectfirst' => ['required', 'array', 'size:10'],
            'questionsectionobjectfirst.*' => ['nullable', 'string', 'max:1000'],
            'questionsectionseond' => ['required', 'array', 'size:10'],
            'questionsectionseond.*' => ['nullable', 'string', 'max:1000'],
            'questionsectionthird' => ['required', 'array', 'size:10'],
            'questionsectionthird.*' => ['nullable', 'string', 'max:1000'],
            'questionsectionfouth' => ['required', 'array', 'size:10'],
            'questionsectionfouth.*' => ['nullable', 'string', 'max:1000'],
            'questionsectionobjectfirsthindi' => ['required', 'array', 'size:10'],
            'questionsectionobjectfirsthindi.*' => ['nullable', 'string', 'max:1000'],
            'questionsectionseondhindi' => ['required', 'array', 'size:10'],
            'questionsectionseondhindi.*' => ['nullable', 'string', 'max:1000'],
            'questionsectionthirdhindi' => ['required', 'array', 'size:10'],
            'questionsectionthirdhindi.*' => ['nullable', 'string', 'max:1000'],
            'questionsectionfouthhindi' => ['required', 'array', 'size:10'],
            'questionsectionfouthhindi.*' => ['nullable', 'string', 'max:1000'],
            'questionobjectanswer' => ['required', 'array', 'size:10'],
            'questionobjectanswer.*' => ['required', Rule::in(['A', 'B', 'C', 'D'])],
        ];
    }

    public function withValidator($validator): void
    {
        $validator->after(function ($validator) {
            if ($this->selectedSubjectId() > 0 && blank($this->input('questionchapter'))) {
                $validator->errors()->add('questionchapter', 'The chapter field is required for the selected subject.');
            }
        });
    }

    public function attributes(): array
    {
        return [
            'questionchapter' => 'chapter',
        ];
    }

    private function selectedSubjectId(): int
    {
        $subjectValue = (string) $this->input('questionsubject', '');

        if (! str_contains($subjectValue, '<=>')) {
            return 0;
        }

        [$subjectId] = explode('<=>', $subjectValue, 2);

        return is_numeric($subjectId) ? (int) $subjectId : 0;
    }
}
