<?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\TrainReservation;
use App\TrainnerReservation;

class TrainnerReservationRequest 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 = TrainReservation::where('trainner_id',$user->id)->where('dating',$request->dating)->where('timing',$request->timing)->first();
        $date = TrainReservation::find($request->id);
        $reservations = TrainnerReservation::where('physical_reservation_id',$request->id)->get();
        
        
        $rules=[
           
            'dating'=> 'required',
            'timing'=> 'required',
            'code_id'=>'required',
            'physcal.*.physcal_id'=>'required',
            'physcal.*.num'=>'required',
            'system_physic_id'=> 'required',
            
            
        ];
        if(count($reservations)=='0'){
            $rule=['count'=>'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'=> __('trainreservations.count'),
            'height'=> __('trainreservations.height'),
            'dating'=> __('trainreservations.dating'),
            'timing'=> __('trainreservations.timing'),
            'width'=> __('trainreservations.width'),
            'weight'=>__('trainreservations.weight'),
            'picked'=>__('trainreservations.picked'),
            'system_food_id'=> __('trainreservations.system_food'),
            'system_physic_id'=> __('trainreservations.system_physic'),
            'expired'=> __('trainreservations.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);
        }
    }
}
