<?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;

class ProductRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if (! Auth::check()) {
            return false;
        }
        if(in_array(Auth::user()->role, ['super_admin', 'admin', 'sub_admin']))
        {
            return true;
        }

        if(Auth::user()->hasModule( 'products' ) )
        {
            return true;
        }
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'is_special' => 'required|in:1,0',
            'show_advantages' => 'required|in:1,0',
            'show_table' => 'required|in:1,0',
        ];

        foreach (languages() as $lang) {
            $lang_rules=[
                'name_'.$lang->local=> 'required|min:3|max:300',
                'description_'.$lang->local=> 'required|min:3',
                'advantages_'.$lang->local=> 'required|min:3',
                'advantage_desc_'.$lang->local=> 'required|min:3|max:300',
                'minimum_title_'.$lang->local=> 'required|min:3|max:300',
                'maximum_title_'.$lang->local=> 'required|min:3|max:300',
            ];
            $rules= array_merge($rules, $lang_rules);
        }


        if (!request('minimum')){
            $rules['all_minimum'] = 'required|array|min:1';
            return  $rules;
        }
        if (!request('maximum')){
            $rules['all_maximum'] = 'required|array|min:1';
            return  $rules;
        }

        foreach (request('minimum') as $minimum) {
            if (!$minimum){
                $rules['all_minimum'] = 'required';
                return  $rules;
            }
        }

        foreach (request('maximum') as $maximum) {
            if (!$maximum){
                $rules['all_maximum'] = 'required';
                return  $rules;
            }
        }

        foreach (request('points')['ar'] as $point) {
            if (!$point){
                $rules['all_points'] = 'required';
                return $rules;
            }
        }

        foreach (request('points')['en'] as $point) {
            if (!$point){
                $rules['all_points'] = 'required';
                return $rules;
            }
        }

        $rules['images'] = 'required|array|min:1';

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $field = metaFields('products');
        $local = getCurrentLocale();

        $attributes=[
            'is_special' => $field[$local]['is_special'] ?? __('products.is_special'),
            'show_points' => $field[$local]['show_points'] ?? __('products.show_points'),
            'show_table' => $field[$local]['show_table'] ?? __('products.show_table'),
            'images' => $field[$local]['images'] ?? __('products.images'),
            'all_minimum' => __('products.minimum'),
            'all_maximum' => __('products.maximum'),
            'all_points' => __('products.points'),
        ];
        foreach (languages() as $lang) {
            $lang_attributes=[
                'name_'.$lang->local=> $field[$lang->local]['name'] ?? __('products.name'),
                'description_'.$lang->local=>  $field[$lang->local]['description'] ?? __('products.description'),
                'advantages_'.$lang->local=> $field[$lang->local]['advantages'] ?? __('products.advantages'),
                'advantage_desc_'.$lang->local=> $field[$lang->local]['advantage_desc'] ?? __('products.advantage_desc'),
                'minimum_title_'.$lang->local=> $field[$lang->local]['minimum_title'] ?? __('products.minimum_title'),
                'maximum_title_'.$lang->local=> $field[$lang->local]['maximum_title'] ?? __('products.maximum_title'),
                "points[$lang->local]" => $field[$lang->local]['points'] ?? __('products.points'),
                ];
            $attributes= array_merge($attributes, $lang_attributes);
        }
        return $attributes;
    }

    public function messages()
    {
        return [];
    }

    /**
     * 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::ajax()){
            if ($validator->fails()){
                $errors = $validator->errors()->first();
                throw new HttpResponseException(response()->json(['errors' => $errors]));
            }

        } else {
            // Do the original action of the Form request class
            parent::failedValidation($validator);
        }
    }
}
