<?php

namespace Modules\Setting\Repositories;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Modules\Setting\Interfaces\SettingRepositoryInterface;
use Modules\Setting\Models\Setting;
use Modules\User\Models\Module;

class SettingRepository implements SettingRepositoryInterface
{

    public function get(Request $request)
    {
        $settings = Setting::cursor();
        return $settings;
    }
    public function getAll()
    {
        $settings = Setting::get()->toArray();
        return $settings;
    }

    public function getItem(Setting $setting)
    {
        return $setting ;
    }
    //
    public function save(array $data):void
    {
        $this->saveData($data);
    }

    private function saveData(array $data ) :void
    {
        foreach ($data as $key => $value) {
            Setting::where(['key' => $key])->update([
                'value' => $value,
            ]);
        }
    }
    public function modules(Request $request):void
    {
        $fields = $request->fields;
        $moduleName = $request->module_name;

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

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

}
