<?php

namespace App\Http\Requests\Web;

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;

class JobApplicationRequest 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()
    {
        $phonePattern = '/^(05){1}([0-9]{8})$/';

        $rules = [
            'name' => 'required|min:3|max:100',
            'email' => 'required|unique:job_applications,email|email:rfc,dns|min:3|max:50',
            'phone' => 'required|unique:job_applications,phone|min:10|max:14|regex:'.$phonePattern,
            'birth_date' => 'required|date',
            'job_name' => 'required|min:3|max:100',
            'country_name' => 'required|exists:country_descriptions,name',
            'cv' => 'required|mimes:jpg,jpeg,png,pdf',
        ];

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $field = metaFields('jobApplications');
        $local = getCurrentLocale();

        $attributes=[
            'name' => $field[$local]['name'] ?? __('jobApplications.name'),
            'email' => $field[$local]['email'] ?? __('jobApplications.email'),
            'phone' => $field[$local]['phone'] ?? __('jobApplications.phone'),
            'birth_date' => $field[$local]['birth_date'] ?? __('jobApplications.birth_date'),
            'job_name' => $field[$local]['job_name'] ?? __('jobApplications.job_name'),
            'country_name' => $field[$local]['country_name'] ?? __('jobApplications.country_name'),
            'cv' => $field[$local]['cv'] ?? __('jobApplications.cv'),
        ];

        return $attributes;
    }

    public function messages()
    {
        return [
            'phone.regex' => __('jobApplications.phone_regex'),
            'email.unique' => __('jobApplications.email_unique'),
            'phone.unique' => __('jobApplications.phone_unique'),
        ];
    }

    /**
     * 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::ajax()){
            if ($validator->fails()){
                $errors = $validator->errors()->first();
                throw new HttpResponseException(response()->json(['errors' => $errors]));
            }

        } else {
            // Do the original action of the Form request class
            parent::failedValidation($validator);
        }
    }
}
