<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
class BranchRequest 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()
    {
        $rules = [
            'name'   => 'required',
            'description' => 'required',
            'address' => 'required',
            'phone' => 'required',
            'lat'=>'required',
            'lan'=>'required',
            'specialty_id'=>'required',
            'details.*.key'=>'required',
            'details.*.value'=>'required',
            'details.*.details'=>'required',
            'day.*.day'=>'required',
            'day.*.start'=>'required',
            'day.*.end'=>'required',
        ];
         if ($this->method() == "POST") {
            $rule = ['image'=>'required'];
            $rules= array_merge($rules, $rule);
        }
        
        return $rules;
    }

    public function attributes()
{
    $details = request('details');
    $days = request('day');
    
    $attributes = [
        'name'=>  __('branches.name'),
        'description'=>  __('branches.description'),
        'address'=>  __('branches.address'),
        'phone'=>  __('branches.phone'),
        'specialty_id'=>  __('branches.specialty'),
        'lat'=>  __('branches.lat'),
        'lan'=>  __('branches.lan'),
        'image'=>__('branches.image')
    ];
    if(!is_null($details)){
    foreach ($details as $key => $value) {
        $serial = ($key + 1);
        if (empty($value['key'])){
            $detailskey = ['details.'.$key.'.key' => "Details $serial Details key"];
            $attributes = array_merge($attributes, $detailskey);
        }
        if (empty($value['value'])){
            $value = ['details.'.$key.'.value' =>  "Details $serial Details value"];
            $attributes = array_merge($attributes, $value);
        }
        if (empty($value['details'])){
            $details = ['details.'.$key.'.details' =>  "Details $serial Details details"];
            $attributes = array_merge($attributes, $details);
        }
       
    }
    }
     if(!is_null($days)){
    foreach ($days as $key => $value) {
        $serial = ($key + 1);
        if (empty($value['day'])){
            $day = ['day.'.$key.'.day' => "Day $serial Day "];
            $attributes = array_merge($attributes, $day);
        }
        if (empty($value['start'])){
            $start = ['day.'.$key.'.start' =>  "Day $serial Day start"];
            $attributes = array_merge($attributes, $start);
        }
        if (empty($value['end'])){
            $end = ['day.'.$key.'.end' =>  "Day $serial Day end"];
            $attributes = array_merge($attributes, $end);
        }
       
    }
    }
    
    return $attributes;
}

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