<?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=[];

        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',
            ];
            $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=[
            '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'),
                "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);
        }
    }
}
