<?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 App\Models;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class ProductRequest extends FormRequest
{
    /**
     * Determine if the writer is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules=[
            
                //'writer'=>'required',
                'category_id'=>'required',
                
                //'link'=>'required',
                //'image'=>'required',
                

        ];
        if(!is_null(request('category_id'))){
            $count = Models::where('category_id',request('category_id'))->count();
            if($count > 0){
            $lang_rules=[
                'model_id'=> 'required',



            ];
            $rules= array_merge($rules, $lang_rules);
            }
            
        }
         foreach (languages() as $lang) {
            $lang_rules=[
                'name_'.$lang->local=> 'required',

               // 'description_'.$lang->local=> 'required',
                


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

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $element = request('element');
       
        $attributes= [
            'category_id'=>  __('products.category'),
            'writer'=>__('products.writer'),
            'model_id'=>__('products.model'),
            'model_id'=>__('products.model'),
            'cost'=>__('products.cost'),
            'image'=>__('products.image'),
            'link'=>__('products.link'),
            'video'=>__('products.video'),
            'latlng'=>__('products.latlng'),
            'start_cost'=>__('products.start_cost'),
            'end_cost'=>__('products.end_cost'),
            'address'=>__('products.address'),
            'country_id'=>__('products.country'),
            'city_id'=>__('products.city'),
            
            
        ];
         foreach (languages() as $lang) {
            $lang_attributes=[
                'name_'.$lang->local => $service->{'name_'.$lang->local} ?? __('products.name'),
                'description_'.$lang->local => $service->{'description_'.$lang->local} ?? __('products.description'),
            ];
            $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 {
            // Do the original action of the Form request class
            parent::failedValidation($validator);
        }
    }
}
