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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules=[
            'consulting_way' => 'required|in:phone,zoom,office',
            'consulting_topic_id' => 'required|exists:consulting_topics,id',
            '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,3,4,7,1,8,5,9,20',
            '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_topic_id'=> $field[$local]['consulting_topic_id'] ?? __('activeConsultings.consulting_topic_id'),
            '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) :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));
    }

}
