<?php

namespace App\Http\Controllers\Admin;

use App\Models\Module;
use App\Models\Setting;
use App\Services\Upload\ImageService;
use Illuminate\View\View;
use Illuminate\Routing\Redirector;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ModuleRequest;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\Admin\SettingRequest;
use Illuminate\Http\RedirectResponse;

class SettingController extends Controller
{
    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath = '/admin/settings';
    /** View folder */
    private $viewDirectory = 'admin.settings.';

    /**
     * Display a listing of the resource.
     * @return View
     */
    public function index() :View
    {
        $title= __('settings.head');
        $head = metaFields('settings', 'add_new', getCurrentLocale()) ?? __('settings.new');

        $settingsAction= route('settings.store');
        $modulesAction= route('settings.Modules');
        $settings = Setting::cursor();
        $modules = Module::where('deleted_at', null)->cursor();

        if(count($settings) > 0){
            $head = metaFields('settings', 'edit', getCurrentLocale()) ?? __('settings.edit');
            foreach ($settings as $setting) {
                $settings->{$setting['key']} = $setting['value'];
            }
        } else {
            $settings = null;
        }

        return view($this->viewDirectory.'form',compact('title', 'head', 'settingsAction','modulesAction', 'settings', 'modules'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param SettingRequest $settingRequest
     * @return RedirectResponse|Redirector
     */
    public function store(SettingRequest $settingRequest) :RedirectResponse
    {
        $this->saveData($settingRequest);
        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }

    /**
     * Store Modules metas in Modules table.
     *
     * @param ModuleRequest $moduleRequest
     * @return RedirectResponse|Redirector
     */
    public function modules(ModuleRequest $moduleRequest) :RedirectResponse
    {
        $fields = $moduleRequest->fields;
        $moduleName = $moduleRequest->module_name;

        $data = array_merge_recursive($moduleName, $fields);

        foreach ($data as $path => $field) {
            Module::where('path', $path)->update([
                'meta' => $field
            ]);
        }

        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }


    /**
     * Handle Save form data
     *
     * @param SettingRequest $request
     * @return void
     */

    private function saveData(SettingRequest $request):void
    {
        $data = $request->all();
        $userRole = Auth::user()->role;

        !$request->has('website_disable') && $userRole == '1' ? $data['website_disable'] = 0 : 1;
        !$request->has('dashboard_disable') && $userRole == 1 ? $data['dashboard_disable'] = 0 : 1;

        if ($request->has('update')){
            foreach ($data as $key => $value) {

                if($key == 'verification_pdf')
                {
                    Setting::where('key', $key)->update([
                        'value' => ImageService::uploadImage($value)
                    ]);
                }
                else
                {
                    Setting::where('key', $key)->update([
                        'value' => $value
                    ]);
                }
            }
        } else {
            foreach ($data as $key => $value) {
                Setting::create([
                    'key' => $key,
                    'value' => $value,
                ]);
            }
        }
    }

}
