<?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 FirstApplyRequest 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 =
            [
                'name'=>'required',
                'phone'=>'required|numeric',
                'email'=>'required|email',
                'gender'=>'required|in:male,female',
                'age'=>'required|numeric',
                'date_of_birth'=>'required|date',
                'national_id'=>'required',
                'nationality'=>'required',
                'marital'=>'required|in:single,married',
                'district'=>'required',
                'process_id.*'=>'required|exists:choises,id',
            ];

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes=
            [
                'name'=> __('web.name'),
                'phone'=> __('web.phone'),
                'email'=> __('web.email'),
                'gender'=> __('web.gender'),
                'age'=> __('web.age'),
                'date_of_birth'=> __('web.date_of_birth'),
                'national_id'=> __('web.national_id'),
                'nationality'=> __('web.nationality'),
                'marital'=> __('web.marital'),
                'district'=> __('web.district'),
                'process_id.*'=> __('web.process_id'),
            ];

        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);
        }
    }
}
