<?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 = [
            'status' => 'nullable|in:0,1',
            'departure_city' => 'required|exists:cities,id',
            'arrival_city' => 'required|exists:cities,id',
//            'umrah_trip_type' => 'required|in:u,ut,uta',
//            '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';
                }
            }
        }

//        }

        if (!request('house_private') && !request('house_group')){
            $rules['house_type'] = 'required';
        }

        $rules['house_private'] = 'nullable|in:private';
        $rules['house_private_price'] = 'required_if:house_private,private|nullable|numeric|gt:0';
        $rules['house_group'] = 'nullable|in:group';
        $rules['house_group_price'] = 'required_if:house_group,group|nullable|numeric|gt:0';
        $rules['days_counts'] = 'required|gt:0';
        $rules['maximum_person_count'] = 'required|numeric|gt:0';

        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['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=[
            'image' => $field[$local]['image'] ?? __('umrahTrips.image'),
            'umrah_trip_type' => $field[$local]['umrah_trip_type'] ?? __('umrahTrips.umrah_trip_type'),
            'umrah_trip_date' => $field[$local]['umrah_trip_date'] ?? __('umrahTrips.umrah_trip_date'),
            'house_private' => $field[$local]['house_private'] ?? __('umrahTrips.house_private'),
            'house_group' => $field[$local]['house_group'] ?? __('umrahTrips.house_group'),
            'all_umrah_trip_date' => $field[$local]['umrah_trip_date'] ?? __('umrahTrips.umrah_trip_date'),
            'house_private_price' => $field[$local]['house_private_price'] ?? __('umrahTrips.house_private_price'),
            'house_group_price' => $field[$local]['house_group_price'] ?? __('umrahTrips.house_group_price'),
            'house_type' => __('umrahTrips.house_type'),
            'house_type_price_required' =>  __('umrahTrips.house_type_price_required'),
            'days_counts' => $field[$local]['days_counts'] ?? __('umrahTrips.days_counts'),
            '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'),
            'maximum_person_count' => $field[$local]['maximum_person_count'] ?? __('umrahTrips.maximum_person_count'),
        ];

        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_private.required_if' => __('umrahTrips.house_private_required_if'),
            'house_private_price.required_if' => __('umrahTrips.house_type_price_required'),
            'house_group_price.required_if' => __('umrahTrips.house_type_price_required'),
            'lat.required_if' => __('umrahTrips.lat_required_if'),
            'lng.required_if' => __('umrahTrips.lng_required_if'),
            'maximum_person_count.gt' => __('umrahTrips.maximum_person_count_gt'),
        ];

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