<?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;

/**
 * @property mixed password
 * @property mixed active_way
 * @property mixed consulting_id
 * @property mixed coupon_code
 */
class ConsultingBookingRequest 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()
    {
        $rules=[
            'consulting_way' => 'required|in:phone,zoom,office',
            'consulting_subject' => 'required|min:3|max:400',
            'consulting_date' => 'required|date|after_or_equal:'.date('Y-m-d'),
            'consulting_time' => 'required|regex:/[0-9]{1,2}:[0-9]{1,2}/',
            'pay_way' => 'required|in:14,11,2,6',
            'coupon_code' => 'nullable|exists:coupons,code',
            'consulting_id' => 'required|exists:consultings,id',
        ];

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $field = metaFields('activeConsultings');
        $local = getCurrentLocale();
        $attributes= [
            'consulting_id'=> $field[$local]['consulting_id'] ?? __('activeConsultings.consulting_id'),
            'consulting_way'=> $field[$local]['consulting_way'] ?? __('activeConsultings.consulting_way'),
            'consulting_date'=> $field[$local]['consulting_date'] ?? __('activeConsultings.consulting_date'),
            'consulting_time'=> $field[$local]['consulting_time'] ?? __('activeConsultings.consulting_time'),
            'consulting_subject'=> $field[$local]['consulting_subject'] ?? __('activeConsultings.consulting_subject'),
            'pay_way'=> $field[$local]['pay_way'] ?? __('activeConsultings.pay_way'),
            'coupon_code'=> $field[$local]['coupon_id'] ?? __('activeConsultings.coupon_id')
        ];
        return $attributes;
    }

    public function messages()
    {
        return [
            'phone.digits' => __('activeConsultings.phone_digits'),
            'phone.regex' => __('activeConsultings.phone_regex'),
        ];
    }

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