<?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 ProjectRequest 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( 'projects' ) )
        {
            return true;
        }
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        if ($this->projectDetail){
            $rules = [];
            foreach (languages() as $lang) {
                $lang_rules=[
                    'title_'.$lang->local=> 'required|min:3|max:100',
                    'description_'.$lang->local=> 'required|min:3',
                ];
                $rules= array_merge($rules, $lang_rules);
            }
        } else {
            $rules=[
                'country_id' => 'required|exists:countries,id',
                'category_id' => 'required|exists:categories,id',
                'project_value' => 'required|gt:0',
                'start_year' => 'required|integer|min:1970|max:'.(date('Y')+1),
                'start_month' => 'required|integer|min:1|max:12',
                'completed_date' => 'nullable|date',
                'client' => 'required|min:3|max:100',
//                'started_date' => 'required|date',
            ];

            foreach (languages() as $lang) {
                $lang_rules=[
                    'name_'.$lang->local=> 'required|min:3|max:100',
                    'keywords_'.$lang->local=> 'required|min:3|max:100',
                    'meta_description_'.$lang->local=> 'required|min:3|max:100',
                    'description_'.$lang->local=> 'required|min:3',

                ];


                if (request('work_scopes_'.$lang->local)){
                    foreach (request('work_scopes_'.$lang->local) as $key => $workScope) {
                        if (!$workScope){
                            $rules['all_work_scopes'] = 'required';
                        }
                    }
                }

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

        $rules['images'] = 'required|array';

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $field = metaFields('projects');
        $local = getCurrentLocale();
        $attributes=[
            'country_id' => $field[$local]['country_id'] ?? __('projects.country_id'),
            'category_id' => $field[$local]['category_id'] ?? __('projects.category_id'),
            'completed_date' => $field[$local]['completed_date'] ?? __('projects.completed_date'),
            'started_date' => $field[$local]['started_date'] ?? __('projects.started_date'),
            'project_value' => $field[$local]['project_value'] ?? __('projects.project_value'),
            'client' => $field[$local]['client'] ?? __('projects.client'),
            'images' => $field[$local]['images'] ?? __('projects.images'),
            'all_work_scopes' => $field[$local]['work_scopes'] ?? __('projects.work_scopes'),
        ];

        if ($this->projectDetail){
            foreach (languages() as $lang) {
                $lang_attributes=[
                    'title_'.$lang->local=> __('projects.page_title'),
                    'description_'.$lang->local => __('projects.page_description'),
                ];

                $attributes= array_merge($attributes, $lang_attributes);
            }
        } else {
            foreach (languages() as $lang) {
                $lang_attributes=[
                    'name_'.$lang->local=> $field[$lang->local]['name'] ?? __('projects.name'),
                    'title_'.$lang->local=> $field[$lang->local]['title'] ?? __('projects.title'),
                    'description_'.$lang->local=> $field[$lang->local]['description'] ?? __('projects.description'),
                    'slug_'.$lang->local=>  $field[$lang->local]['slug'] ?? __('projects.slug'),
                    'keywords_'.$lang->local=> $field[$lang->local]['keywords'] ?? __('projects.keywords'),
                    'meta_description_'.$lang->local=> $field[$lang->local]['meta_description'] ?? __('projects.meta_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::ajax()){
            $errors = $validator->errors()->first();
            throw new HttpResponseException(response()->json(['errors' => $errors]));
        } else {
            parent::failedValidation($validator);
        }
    }
}
