<?php

namespace App\Http\Requests\Admin;

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

class StoreCompetitionAssignmentRequest extends FormRequest
{
    protected function prepareForValidation(): void
    {
        $examTime = (string) $this->input('examtimeing', '');

        if ($examTime !== '' && preg_match('/^\d{2}:\d{2}:\d{2}$/', $examTime) === 1) {
            $this->merge([
                'examtimeing' => substr($examTime, 0, 5),
            ]);
        }
    }

    public function authorize(): bool
    {
        return auth()->check();
    }

    public function rules(): array
    {
        return [
            'questiontitle' => ['required', 'string', 'max:2000', Rule::unique('compitetionassignment', 'compitetiontitle')],
            'questiongoupsclass' => ['required', 'string', 'max:255'],
            'questionboard' => ['required', 'string', 'max:255'],
            'examdate' => ['required', 'date'],
            'resultdate' => ['required', 'date', 'after_or_equal:examdate'],
            'examtimeing' => ['required', 'date_format:H:i'],
            'startdate' => ['required', 'date', 'before_or_equal:enddate'],
            'enddate' => ['required', 'date', 'after_or_equal:startdate', 'before_or_equal:examdate'],
            'answersheetdate' => ['required', 'date', 'after_or_equal:examdate', 'before_or_equal:resultdate'],
            'questionset' => ['required', 'array', 'min:1'],
            'questionset.*' => ['required', 'string', 'max:255'],
            'questioncompetition' => ['required', 'string', 'max:255'],
        ];
    }

    public function messages(): array
    {
        return [
            'examtimeing.date_format' => 'Exam end time HH:MM format mein select kijiye.',
            'startdate.before_or_equal' => 'Competition start date end date ke baad nahin ho sakti.',
            'enddate.after_or_equal' => 'Competition end date start date se pehle nahin ho sakti.',
            'enddate.before_or_equal' => 'Competition end date exam date ke baad nahin ho sakti.',
            'resultdate.after_or_equal' => 'Result date exam date ke barabar ya uske baad honi chahiye.',
            'answersheetdate.after_or_equal' => 'Answer sheet display date exam date se pehle nahin ho sakti.',
            'answersheetdate.before_or_equal' => 'Answer sheet display date result date ke baad nahin ho sakti.',
            'questionset.min' => 'Kam se kam ek question set select kijiye.',
        ];
    }

    public function attributes(): array
    {
        return [
            'questiontitle' => 'assignment title',
            'questiongoupsclass' => 'class group',
            'questionboard' => 'board pattern',
            'examdate' => 'exam date',
            'resultdate' => 'result date',
            'examtimeing' => 'exam end time',
            'startdate' => 'competition start date',
            'enddate' => 'competition end date',
            'answersheetdate' => 'answer sheet display date',
            'questionset' => 'question set',
            'questioncompetition' => 'competition',
        ];
    }
}
