<?php

namespace App\Http\Requests\API;

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 password
 * @property mixed active_way
 * @property mixed documentation_id
 * @property mixed coupon_code
 * @property mixed type
 * @property mixed city_id
 */
class TrainingsRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
//        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $phonePattern = '/^(05){1}([0-9]{8})$/';
        $rules=[
            'purpose_training_id' => 'required|exists:purpose_trainings,id',
            'name' => 'required|min:3|max:50',
            'email' => 'required|email|max:50',
            'university' => 'nullable|min:3|max:100',
            'phone' => 'required|numeric|regex:'.$phonePattern,
            'birth_date' => 'required|date',
            'graduation_date' => 'nullable|date',
            'cv' => 'required|array|min:1',
            'certifications' => 'required|array|min:1',
        ];
        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $field = metaFields('trainingRequests');
        $local = getCurrentLocale();
        $attributes= [
            'purpose_training_id'=> $field[$local]['purpose_training_id'] ?? __('trainingRequests.purpose_training_id'),
            'name'=> $field[$local]['name'] ?? __('trainingRequests.name'),
            'email'=> $field[$local]['email'] ?? __('trainingRequests.email'),
            'birth_date'=> $field[$local]['birth_date'] ?? __('trainingRequests.birth_date'),
            'graduation_date'=> $field[$local]['graduation_date'] ?? __('trainingRequests.graduation_date'),
            'university'=> $field[$local]['university'] ?? __('trainingRequests.university'),
            'phone'=> $field[$local]['phone'] ?? __('trainingRequests.phone'),
            'cv'=> $field[$local]['cv'] ?? __('trainingRequests.cv'),
            'certifications'=> $field[$local]['certifications'] ?? __('trainingRequests.certifications'),
        ];
        return $attributes;
    }

    public function messages()
    {
        return [
            'phone.digits' => __('trainingRequests.phone_digits'),
//            'phone.regex' => __('trainingRequests.phone_regex'),
            'city_id.required_if' => __('trainingRequests.city_id_required'),
        ];
    }

    /**
     * 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) :void
    {
        // Change response attitude if the request done via Ajax requests like API requests
        $errors = (new ValidationException($validator))->errors();
        $message = $validator->errors()->first();
        throw new HttpResponseException(response()->json(['message'=> $message, 'errors' => $errors],  400));
    }
}
