<?php

namespace Modules\Setting\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\Setting\Enums\SettingTaxValidEnum;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $tax_valid_statuses = SettingTaxValidEnum::values();
        $rules=[
            'website_name' => 'required',
            'keywords' => 'required',
            'meta_description' => 'required',
            'site_email' => 'required|email',
            'site_address_ar' => 'required',
            'site_address_en' => 'required',
            'site_phone' => 'required|required|regex:/^([\+00\-]?[0-9]{1,3}[0-9\s]{8,16})$/',
            'mail_driver' => 'required',
            'mail_host' => 'required',
            'mail_port' => 'required|numeric',
            'mail_username' => 'required',
            'mail_password' => 'required',
            'mail_encryption' => 'sometimes',
            'mail_from_address' => 'required',
            'mail_from_name' => 'required',
            'facebook' => 'nullable|url',
            'x' => 'nullable|url',
            'instagram' => 'nullable|url',
            'snapchat' => 'nullable|url',
            'whatsapp' => 'nullable|numeric',
            'tax_valid' => 'nullable|in:'.implode(',',$tax_valid_statuses),
            'tax_value' => 'nullable|numeric',
            'map_link' => 'required|url',
            'website_logo' => 'sometimes',
            'website_disable' => 'sometimes',
            'dashboard_disable' => 'sometimes',
        ];


        return $rules;
    }

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

        $attributes =  [
            'website_name' => $field[$local]['website_name'] ?? __('settings.website_name'),
            'keywords' => $field[$local]['keywords'] ?? __('settings.keywords'),
            'meta_description' => $field[$local]['meta_description'] ?? __('settings.meta_description'),
            'site_email' => $field[$local]['site_email'] ?? __('settings.site_email'),
            'site_address' => $field[$local]['site_address'] ?? __('settings.site_address'),
            'site_phone' => $field[$local]['site_phone'] ?? __('settings.site_phone'),
            'mail_driver' => $field[$local]['mail_driver'] ?? __('settings.mail_driver'),
            'mail_host' => $field[$local]['mail_host'] ?? __('settings.mail_host'),
            'mail_port' => $field[$local]['mail_port'] ?? __('settings.mail_port'),
            'mail_username' => $field[$local]['mail_username'] ?? __('settings.mail_username'),
            'mail_password' => $field[$local]['mail_password'] ?? __('settings.mail_password'),
            'mail_encryption' => $field[$local]['mail_encryption'] ?? __('settings.mail_encryption'),
            'mail_from_address' => $field[$local]['mail_from_address'] ?? __('settings.mail_from_address'),
            'mail_from_name' => $field[$local]['mail_from_name'] ?? __('settings.mail_from_name'),
            'facebook' => $field[$local]['facebook'] ?? __('settings.facebook'),
            'x' => $field[$local]['x'] ?? __('settings.x'),
            'snapchat' => $field[$local]['snapchat'] ?? __('settings.snapchat'),
            'whatsapp' => $field[$local]['whatsapp'] ?? __('settings.whatsapp'),
            'tax_valid' => $field[$local]['tax_valid'] ?? __('settings.tax_valid'),
            'tax_value' => $field[$local]['tax_value'] ?? __('settings.tax_value'),
            'map_url' => $field[$local]['map_url'] ?? __('settings.map_url'),
            'instagram' => $field[$local]['instagram'] ?? __('settings.instagram'),
            'linkedin' => $field[$local]['linkedin'] ?? __('settings.linkedin'),
            'quote_email' => $field[$local]['quote_email'] ?? __('settings.quote_email'),
            'website_logo' => $field[$local]['website_logo'] ?? __('settings.website_logo'),
        ];
        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);
        }
    }
}
