<?php

namespace App\Http\Requests\Web;

use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

/**
 * @property mixed video_type
 * @property mixed video_url
 * @property mixed images
 */
class CompleteApplyRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules =
            [
                'course_id'=>'required|exists:products,id',
                'study_level'=>'required',
                'words_work_experience'=>'required',
                'goal_year'=>'required',
                'work_experience'=>'required',
                'subject_training_session'=>'required',
                'training_session'=>'required',
                'pros_cons'=>'required',
                'social_problem'=>'required',
                'volunteered'=>'required',
                'attend_programs_training'=>'required',
                'courses_nesma'=>'required',
                'know_nesma_training'=>'required',
                'cv'=>'required|mimes:png,jpg,jpeg,pdf',
            ];


        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes=
            [
                'course_id'=>__('web.course_id'),
                'study_level'=>__('web.study_level'),
                'words_work_experience'=>__('web.words_work_experience'),
                'goal_year'=>__('web.goal_year'),
                'work_experience'=>__('web.work_experience'),
                'subject_training_session'=>__('web.subject_training_session'),
                'training_session'=>__('web.training_session'),
                'pros_cons'=>__('web.pros_cons'),
                'social_problem'=>__('web.social_problem'),
                'volunteered'=>__('web.volunteered'),
                'attend_programs_training'=>__('web.attend_programs_training'),
                'courses_nesma'=>__('web.courses_nesma'),
                'know_nesma_training'=>__('web.know_nesma_training'),
                'cv'=>__('web.cv'),
            ];

        return $attributes;
    }


    /**
     * Handle a failed validation attempt.
     *
     * Override the parent method action if the request is for AJAX
     *
     * @param Validator $validator
     * @return void
     *
     * @throws ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        // Change response attitude if the request done via Ajax requests like API requests
        if(Request::wantsJson()){
            $errors = (new ValidationException($validator))->errors();
            throw new HttpResponseException(response()->json(['errors' => $errors], 422));
        } else {
            parent::failedValidation($validator);
        }
    }
}
