<?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 UmrahTripRequest 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('umrahTrips'))
        {
            return true;
        }
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
//            'umrah_trip_type' => 'required|in:u,ut,uta',
            'umrah_trip_date' => 'required|array',
            'price_per_person' => 'required|not_in:0|gt:0',
            'status' => 'nullable|in:0,1',
            'departure_city' => 'required|exists:cities,id',
            'arrival_city' => 'required|exists:cities,id',
//            'point_address.*' => 'required_if:umrah_trip_type,ut,uta',
//            'lat' => 'required_if:umrah_trip_type,ut,uta|array',
//            'lng' => 'required_if:umrah_trip_type,ut,uta|array',
        ];

        if (request('umrah_trip_date') ){
            foreach (request('umrah_trip_date') as $key => $umrahTripDate) {
                if (!$umrahTripDate){
                    $rules["all_umrah_trip_date"] = 'required';
                }
            }
        }

//        if (in_array(request('umrah_trip_type'), ['ut', 'uta'])){

        if (request('point_address') ){
            foreach (request('point_address') as $key => $address) {
                if (!$address){
                    $rules["all_point_address"] = 'required';
                }
            }
        }
        if (request('lat') ){
            foreach (request('lat') as $key => $address) {
                if (!$address){
                    $rules["all_lat"] = 'required';
                }
            }
        }
        if (request('lng') ){
            foreach (request('lng') as $key => $address) {
                if (!$address){
                    $rules["all_lng"] = 'required';
                }
            }
        }

//        }


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


        $rules['house_type'] = 'required';
        $rules['image'] = 'required';


//        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('umrahTrips');
        $local = getCurrentLocale();
        $attributes=[
            'umrah_trip_type' => $field[$local]['umrah_trip_type'] ?? __('umrahTrips.umrah_trip_type'),
            'umrah_trip_date' => $field[$local]['umrah_trip_date'] ?? __('umrahTrips.umrah_trip_date'),
            'all_umrah_trip_date' => $field[$local]['umrah_trip_date'] ?? __('umrahTrips.umrah_trip_date'),
            'price_per_person' => $field[$local]['price_per_person'] ?? __('umrahTrips.price_per_person'),
            'status' => $field[$local]['status'] ?? __('umrahTrips.status'),
            'departure_city' => $field[$local]['departure_city'] ?? __('umrahTrips.departure_city'),
            'arrival_city' => $field[$local]['arrival_city'] ?? __('umrahTrips.arrival_city'),
            'point_address' => $field[$local]['point_address'] ?? __('umrahTrips.point_address'),
            'all_point_address' => $field[$local]['point_address'] ?? __('umrahTrips.point_address'),
            'lat' => $field[$local]['lat'] ?? __('umrahTrips.lat'),
            'all_lat' => $field[$local]['lat'] ?? __('umrahTrips.lat'),
            'lng' => $field[$local]['lng'] ?? __('umrahTrips.lng'),
            'all_lng' => $field[$local]['lng'] ?? __('umrahTrips.lng'),
            'house_type' => $field[$local]['house_type'] ?? __('umrahTrips.house_type'),
        ];

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

    public function messages()
    {

        $messages = [
            'departure_city.required_if' => __('umrahTrips.departure_city_required_if'),
            'arrival_city.required_if' => __('umrahTrips.arrival_city_required_if'),
            'point_address.required_if' => __('umrahTrips.point_address_required_if'),
            'house_type.required_if' => __('umrahTrips.house_type_required_if'),
            'lat.required_if' => __('umrahTrips.lat_required_if'),
            'lng.required_if' => __('umrahTrips.lng_required_if'),
        ];

        foreach (request('point_address') as $key => $point) {
            $messages["point_address.$key.required_if"] = __('umrahTrips.point_address_required_if');
        }
        foreach (request('lat') as $key => $price) {
            $messages["lat.$key.required_if"] = __('umrahTrips.lat_required_if');
        }
        foreach (request('lng') as $key => $price) {
            $messages["lng.$key.required_if"] = __('umrahTrips.lng_required_if');
        }
        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);
	    }
    }
}
