<?php

namespace Modules\Page\Requests\Admin;

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;
use Modules\User\Enums\UserRoleEnum;

class PageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $role = UserRoleEnum::admins();
        if (!Auth::check()) {
            return false;
        }
        if (!in_array(Auth::user()->role, $role)) {
            return false;
        }
        if (Auth::user()->hasModule('pages')) {
            return true;
        }
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $imageRule = ($this->page ? 'nullable' : 'required') . '|mimes:jpg,jpeg,png,gif,svg,svgz|max:51200';
        $rules = [
            'image' => $imageRule,
        ];

        if ($this->page && $this->page->id == 10) {
            $rules['value_1'] = 'required|numeric';
            $rules['value_2'] = 'required|numeric';
            $rules['value_3'] = 'required|numeric';
            $rules['value_4'] = 'required|numeric';
            $rules['image_1'] = 'nullable|mimes:jpg,jpeg,png,gif,svg,svgz|max:51200';
            $rules['image_2'] = 'nullable|mimes:jpg,jpeg,png,gif,svg,svgz|max:51200';
            $rules['image_3'] = 'nullable|mimes:jpg,jpeg,png,gif,svg,svgz|max:51200';
            $rules['image_4'] = 'nullable|mimes:jpg,jpeg,png,gif,svg,svgz|max:51200';
        }

        foreach (languages() as $lang) {
            $lang_rules = [];
            $lang_rules = [
                'title_' . $lang->local => 'required|max:150',
                // 'description_' . $lang->local => $this->page->id === 9 ? 'nullable' : 'required',
            ];

            if (!$this->page || ($this->page->id != 10 )) {
                $lang_rules =
                    [

                        'description_' . $lang->local => 'required|min:3|max:1000',
                    ];
            } else {
                $lang_rules =
                    [
                        'title_2_' . $lang->local => 'required|min:3|max:80',
                        'title_3_' . $lang->local => 'required|min:3|max:80',
                        'title_4_' . $lang->local => 'required|min:3|max:80',
                    ];
            }
            $rules = array_merge($rules, $lang_rules);
        }


        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {

        $attributes = [
            'image' => __('pages.image'),
            'image_2' => __('pages.image') . ' (2)',
            'image_3' => __('pages.image') . ' (3)',
            'image_4' => __('pages.image') . ' (4)',
            'value_1' => __('pages.value_1'),
            'value_2' => __('pages.value_2'),
            'value_3' => __('pages.value_3'),
            'value_4' => __('pages.value_4'),
        ];
        foreach (languages() as $lang) {
            $lang_attributes = [
                'title_' . $lang->local => __('pages.title') . ' ' . $lang->local,
                'title_2_' . $lang->local => __('pages.title') . ' 2 ' . $lang->local,
                'title_3_' . $lang->local => __('pages.title') . ' 3 ' . $lang->local,
                'title_4_' . $lang->local => __('pages.title') . ' 4 ' . $lang->local,
                'description_' . $lang->local => __('pages.description') . ' ' . $lang->local,
            ];
            $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);
        }
    }
}

