<?php

namespace Modules\User\Requests\Web;

use Illuminate\Validation\Rule;
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 video_type
 * @property mixed video_url
 * @property mixed images
 */
class RegistRequest 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()
    {
        $rules =
            [
                'name'=> 'required|max:100',
                'email'=>[
                    'required',
                    'email',
                    'unique:users,email'
                ],
                
                'min_order'=>[
                    'required',
                    'numeric',
                    'min:1'
                ],
                'category_id'=>[
                    'required',
                    'exists:categories,id'
                ],
                'sub_category_id'=>[
                    'required',
                    'exists:categories,id'
                ],
                'logo'=>[
                    'required',
                    'image',
                    'mimes:jpg,jpeg,png'
                ],
                'header'=>[
                    'required',
                    'image',
                    'mimes:jpg,jpeg,png'
                ],
                'branch.name'=>[
                    'required',
                    'string'
                ],
                'branch.address'=>[
                    'required',
                    'string'
                ],
                'branch.cost_delivery'=>[
                    'required',
                    'numeric',
                ],
                'branch.time_delivery'=>[
                    'required',
                    'numeric',
                ],
                'branch.time_prepare'=>[
                    'required',
                    'numeric',
                ],
                'branch.city_id'=>[
                    'required',
                    'exists:cities,id',
                ],
                'branch.area_id'=>[
                    'required',
                    'exists:areas,id',
                ],
                'provider.name'=>[
                    'required',
                    'string'
                ],
                'provider.email'=>[
                    'required',
                    'email'
                ],
                'provider.phone'=>[
                    'required',
                    'numeric'
                ],
                'provider.password'=>[
                    'required',
                    'confirmed'
                ],
                'bank_account'=>[
                    'required',
                    'string'
                ],
                'bank_num'=>[
                    'required',
                    'string'
                ],
                'iban'=>[
                    'required',
                    'string'
                ],
                'bank'=>[
                    'required',
                    'string'
                ],
                'user_id_front_image'=>[
                    'required',
                    'image',
                    'mimes:jpg,jpeg,png'
                ],
                'user_id_back_image'=>[
                    'required',
                    'image',
                    'mimes:jpg,jpeg,png'
                ],
                'iban_card'=>[
                    'required',
                    'file',
                    'mimes:jpg,jpeg,png,pdf'
                ],
                'license_work'=>[
                    'required',
                    'file',
                    'mimes:jpg,jpeg,png,pdf'
                ],
              
            ];
        $request = request();
        if(!isset($request->latitude) || !isset($request->longitude) )
        {
            $rules['map'] = 'required';
        }

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes=
            [
                'name'=>  __('web.name'),
                'email'=>  __('web.email'),
                'min_order'=>  __('web.min_order'),
                'category_id'=>  __('web.category'),
                'sub_category_id'=>  __('web.sub_category_id'),
                'logo'=>  __('web.logo'),
                'header'=>  __('web.header'),
                'password'=>  __('web.password'),
                'password_confirmation'=> __('web.password_confirmation'),
                'branch.name'=> __('web.addresses_data.name'),
                'branch.address'=> __('web.addresses_data.address'),
                'branch.cost_delivery'=> __('web.addresses_data.cost_delivery'),
                'branch.time_delivery'=> __('web.addresses_data.time_delivery'),
                'branch.time_prepare'=> __('web.addresses_data.time_prepare'),
                'branch.city_id'=> __('web.city'),
                'branch.area_id'=> __('web.area'),
                'provider.name'=> __('web.providers_data.name'),
                'provider.email'=> __('web.providers_data.email'),
                'provider.phone'=> __('web.providers_data.phone'),
                'provider.password'=> __('web.password'),
                'bank_account'=> __('web.bank_account'),
                'bank_num'=> __('web.bank_num'),
                'iban'=> __('web.iban'),
                'bank'=> __('web.bank'),
                'user_id_front_image'=> __('web.user_id_front_image'),
                'user_id_back_image'=> __('web.user_id_back_image'),
                'iban_card'=> __('web.iban_card'),
                'license_work'=> __('web.license_work'),
                'map'=> __('web.map'),
                'logo'=> __('web.provider_logo'),
                'header'=> __('web.provider_header'),
            ];

        return $attributes;
    }


    /**
     * 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::wantsJson()){
            $errors = (new ValidationException($validator))->errors();
            throw new HttpResponseException(response()->json(['errors' => $errors], 422));
        } else {
            parent::failedValidation($validator);
        }
    }
}
