<?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 documentation_id
 * @property mixed coupon_code
 * @property mixed type
 * @property mixed city_id
 */
class CompanyRequest 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=[
            'request_type' => 'required|in:case_study,drafting_contracts,case_study_attending',
            'case_topic_id' => 'required|exists:case_topics,id',
//            'subject' => 'required|min:3|max:200',
            'requests' => 'required|min:3|max:1000',
            'details' => 'required|min:3|max:1000',
            'files' => 'required|array|min:1',
        ];
        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $field = metaFields('caseRequests');
        $local = getCurrentLocale();
        $attributes= [
            'request_type'=> $field[$local]['request_type'] ?? __('caseRequests.request_type'),
            'case_topic_id'=> $field[$local]['case_topic_id'] ?? __('caseRequests.case_topic_id'),
            'subject'=> $field[$local]['subject'] ?? __('caseRequests.subject'),
            'requests'=> $field[$local]['requests'] ?? __('caseRequests.requests'),
            'details'=> $field[$local]['details'] ?? __('caseRequests.details'),
            'files'=> $field[$local]['files'] ?? __('caseRequests.files'),
        ];
        return $attributes;
    }

    public function messages()
    {
        return [
            'phone.digits' => __('caseRequests.phone_digits'),
            'phone.regex' => __('caseRequests.phone_regex'),
            'city_id.required_if' => __('caseRequests.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)
    {
        // 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);
        }
    }
}
