<?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;
use App\Reservation;
use App\DoctorReservation;

class DoctorReservationRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
   public function authorize()
    {
        $role = [NULL];
        if (! Auth::check()) {
            return false;
        }
        if(in_array(Auth::user()->role_id, $role))
        {
            return false;
        }
        if(Auth::user()->hasModule('users'))
        {
            return true;
        }
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $request = request();
        $user = \Auth::user();
       
        $reservation = Reservation::where('doctor_id',$user->id)->where('dating',$request->dating)->where('timing',$request->timing)->first();
        $date = Reservation::find($request->id);
        $reservations = DoctorReservation::where('doctor_reservation_id',$request->id)->get();
        
        
        $rules=[
           
            'dating'=> 'required',
            'timing'=> 'required',
            'meal.*.meal_id'=> 'required',
            'meal.*.type'=> 'required',
            'image_ultra.*'=>'required',
            
            
        ];
        if(isset($request->system_food_id)){
             $rule=['system_food_id'=> 'required',
             'category_id'=> 'required',
             'code_id'=> 'required',
            'calorie_id'=> 'required'];
            $rules = array_merge($rules, $rule);
        }
        if(count($reservations)=='0'){
            $rule=['count'=>'required','system_food_id'=> 'required',
             'category_id'=> 'required',
             'code_id'=> 'required',
            'calorie_id'=> 'required'];
           $rules = array_merge($rules, $rule);
        }
       if($reservation){
           $rule=['picked'=>'required'];
           $rules = array_merge($rules, $rule);
       }
       if($date->expired == '1'){
           $rules=['expired'=>'required'];
       }
        if($request->type == '0'){
           $rules=['dating'=> 'required',
            'timing'=> 'required',];
       }

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        
        $attributes= [
            'count'=> __('reservations.count'),
            'height'=> __('reservations.height'),
            'dating'=> __('reservations.dating'),
            'timing'=> __('reservations.timing'),
            'width'=> __('reservations.width'),
            'weight'=>__('reservations.weight'),
            'picked'=>__('reservations.picked'),
            'system_food_id'=> __('reservations.system_food'),
            'system_physic_id'=> __('reservations.system_physic'),
            'expired'=> __('reservations.expired'),
            
        
        ];
        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 {
            // Do the original action of the Form request class
            parent::failedValidation($validator);
        }
    }
}
