<?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 ServiceRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if (! Auth::check()) {
            return false;
        }
        if(Auth::user()->role == 'admin' )
        {
            return true;
        }

        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
   /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
       
        $data = explode('/',$this->url('member'));
        $id =intval( end($data));

        if ($id>0) {
             $rules = [];
              foreach (languages() as $lang) {
            $lang_rules=[
                'service.'.$lang->local.'.name'=> 'required',
                'service.'.$lang->local.'.description'=> 'required',
                'form.*.'.$lang->local.'.question'=> 'required',

            ];
            $rules= array_merge($rules, $lang_rules);
        }
        }
        else{
         $rules = [
            'img'=>'required',
         ];
         foreach (languages() as $lang) {
            $lang_rules=[
                'service.'.$lang->local.'.name'=> 'required',
                'service.'.$lang->local.'.description'=> 'required',
                'form.*.'.$lang->local.'.question'=> 'required',

            ];
            $rules= array_merge($rules, $lang_rules);
        }
    }
       return $rules;
    }

    public function attributes()
{
    $data = explode('/',$this->url('member'));
        $id =intval( end($data));
         $forms = request('form');
         $attributes = [
            'img.required'=>__('services.img'),
         ];

       
   
    foreach (languages() as $lang) {
            $lang_attributes=[
                'service.'.$lang->local.'.name'=> $service->{'name_'.$lang->local} ?? __('services.name'),
                'service.'.$lang->local.'.description'=> $service->{'description'.$lang->local} ?? __('services.description'), 
            ];
            $attributes= array_merge($attributes, $lang_attributes);

if ($forms) {
      
    foreach ($forms as $key => $value) {
         
        $serial = ($key + 1);
        if (empty($value['question'])){
            $question = ['form.'.$key.'.'.$lang->local.'.question' =>  "form $lang->local $serial question"];
            $attributes = array_merge($attributes, $question);
        }
        
    }
}
        }

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