<?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;

/**
 * @property mixed video_type
 * @property mixed video_url
 * @property mixed images
 */
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=[
            'hotel_id' => 'sometimes',
        ];



        if (!request()->hotel_id){

            $rules['category_id'] = 'required';
            $rules['distance_from_haram'] = 'required|numeric';
            $rules['floors_number'] = 'required|numeric';
            $rules['rooms_number'] = 'required|numeric';
            $rules['beds_number'] = 'required|numeric';
            $rules['phone'] = 'required|digits_between:9,14';
            $rules['map'] = 'required';
            $rules['images'] = 'required|array|min:3';

            foreach (languages() as $lang) {
                $lang_rules=[
                    'name_'.$lang->local=> 'required|min:3|max:200',
                    'slug_'.$lang->local=> [
                        "required",
                        Rule::unique('article_descriptions', 'slug')->ignore($this->article? $this->article->id : 0, 'hotel_id')
                    ],
                    'keywords_'.$lang->local=> 'required|min:3',
                    'meta_description_'.$lang->local=> 'required|min:3',
                    'description_'.$lang->local=> 'required|min:3',
                    'more_info_'.$lang->local=> 'required|min:3',
                    'facilities_'.$lang->local=> 'required|min:3',
                ];
                $rules= array_merge($rules, $lang_rules);
            }
        } else {
            $rules['images'] = 'required|array|min:1|max:1';
            foreach (languages() as $lang) {
                $lang_rules=[
                    'name_'.$lang->local=> 'required|min:3',
                ];
                $rules= array_merge($rules, $lang_rules);
            }
        }


        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $field = metaFields('hotels');
        $local = getCurrentLocale();
        $attributes=[
            'distance_from_haram' => metaFields('hotels', 'distance_from_haram', $local),
            'floors_number' => metaFields('hotels', 'floors_number', $local),
            'rooms_number' => metaFields('hotels', 'rooms_number', $local),
            'beds_number' => metaFields('hotels', 'beds_number', $local),
            'phone' => metaFields('hotels', 'phone', $local),
            'map' => metaFields('hotels', 'map', $local),
            'category_id' =>  metaFields('hotels', 'category_id', $local) ??__('hotels.category'),
            'hotel_id' =>  metaFields('hotels', 'hotel_id', $local) ??__('hotels.hotel_id'),
            'images' =>  metaFields('hotels', 'images', $local) ??__('hotels.images'),
        ];


        foreach (languages() as $lang) {
            $lang_attributes=[
                'name_'.$lang->local => $field[$lang->local]['name'] ?? __('hotels.name'),
                'slug_'.$lang->local =>  $field[$lang->local]['slug'] ?? __('hotels.slug'),
                'keywords_'.$lang->local => $field[$lang->local]['keywords'] ?? __('hotels.keywords'),
                'meta_description_'.$lang->local=> $field[$lang->local]['meta_description'] ?? __('hotels.meta_description'),
                'description_'.$lang->local=> $field[$lang->local]['description'] ?? __('hotels.description'),
                'more_info_'.$lang->local=> $field[$lang->local]['more_info'] ?? __('hotels.more_info'),
                'facilities_'.$lang->local=> $field[$lang->local]['facilities'] ?? __('hotels.facilities'),
            ];
            $attributes= array_merge($attributes, $lang_attributes);
        }
        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);
        }
    }
}
