<?php

namespace App\Http\Requests;

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;

class HotelRequest extends FormRequest
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if (! Auth::check()) {
            return false;
        }
        if(in_array(Auth::user()->role, ['super_admin', 'admin', 'sub_admin']))
        {
            return true;
        }

        if(Auth::user()->hasModule('hotels'))
        {
            return true;
        }
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'address' => 'required|min:3|max:200',
            'lat' => 'required',
            'lng' => 'required',
            'city_id' => 'required|exists:cities,id',
            'email' => 'required|email|max:100',
            'status' => 'nullable|in:0,1',
            'added_service' => 'required|array',
        ];

        if (request('added_service')){
            foreach (request('added_service') as $key => $service) {
                if (!request('added_service_price')[$service]){
                    $rules["added_service_price.$key"] = 'required|not_in:0|gt:0';
                }
            }
        }

        $rules['room_type'] = 'required|array';

        if (request('room_type')){
            foreach (request('room_type') as $key => $type) {
                if (!request('room_type_price')[$type]){
                    $rules["room_type_price.$key"] = 'required|not_in:0|gt:0';
                }
            }
        }


        foreach (languages() as $lang) {
            $lang_rules=[
                'name_'.$lang->local=> 'required|min:3|max:100',
            ];
            $rules= array_merge($rules, $lang_rules);
        }


        $rules['images'] = 'required|array';


//        dd(request('added_service_price'));

//        dd(request('room_type'), request('room_type_price'));
//        dd($rules);
        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {

        $field = metaFields('hotels');
        $local = getCurrentLocale();
        $attributes=[
            'address' => $field[$local]['address'] ?? __('hotels.address'),
            'lat' => $field[$local]['lat'] ?? __('hotels.lat'),
            'lng' => $field[$local]['lng'] ?? __('hotels.lng'),
            'city_id' => $field[$local]['city_id'] ?? __('hotels.city_id'),
            'email' => $field[$local]['email'] ?? __('hotels.email'),
            'hotel_status' => $field[$local]['hotel_status'] ?? __('hotels.hotel_status'),
            'added_service' => $field[$local]['added_service'] ?? __('hotels.added_service'),
            'added_service_price' => $field[$local]['added_service_price'] ?? __('hotels.added_service_price'),
            'all_added_service_price' => $field[$local]['added_service_price'] ?? __('hotels.added_service_price'),
            'room_type' => $field[$local]['room_type'] ?? __('hotels.room_type'),
            'room_type_price' => $field[$local]['room_type_price'] ?? __('hotels.room_type_price'),
            'images' => $field[$local]['images'] ?? __('hotels.images'),
            'hall_status' => $field[$local]['hall_status'] ?? __('hotels.status'),
        ];

        foreach (languages() as $lang) {
            $lang_attributes=[
                'name_'.$lang->local=> $field[$lang->local]['name'] ?? __('hotels.name'),
            ];
            $attributes= array_merge($attributes, $lang_attributes);
        }

        if (request('added_service')){
            foreach (request('added_service') as $key => $price) {
                $attributes["added_service_price.$key"] = __('hotels.added_service_price');
            }
        }

        if (request('room_type')){
            foreach (request('room_type') as $key => $type) {
                $attributes["room_type_price.$key"] = __('hotels.room_type_price');
            }
        }


        return $attributes;
    }

    public function messages()
    {

        $messages = [];

        foreach (request('added_service_price') as $key => $price) {
            $messages["added_service_price.$key.gt"] = __('hotels.added_service_price_gt');
        }
        foreach (request('room_type_price') as $key => $price) {
            $messages["room_type_price.$key.gt"] = __('hotels.room_type_price_gt');
        }
        return $messages;
    }


    /**
     * 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()){
	        $errors = $validator->errors()->first();
	        throw new HttpResponseException(response()->json(['errors' => $errors]));
	    } else {
	    	// Do the original action of the Form request class
	    	parent::failedValidation($validator);
	    }
    }
}
