<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class SettingsController extends Controller
{
    public function index()
    {
        // Check permission
       if(auth()->user()->can('setting')){
           
            $data = Setting::get();
    
            $settings = [];
            $settings['general'] = [];
            foreach ($data as $d) {
                if ($d->group == '' || $d->group == 'general') {
                    $settings['general'][] = $d;
                } else {
                    $settings[$d->group][] = $d;
                }
            }
            if (count($settings['general']) == 0) {
                unset($settings['general']);
            }
    
            $groups_data = Setting::select('group')->distinct()->get();
            $groups = [];
            foreach ($groups_data as $group) {
                if ($group->group != '') {
                    $groups[] = $group->group;
                }
            }
    
            $active = (request()->session()->has('setting_tab')) ? request()->session()->get('setting_tab') : old('setting_tab', key($settings));
            return view('settings.index', compact('settings', 'groups', 'active'));
       }
       else{
       dd('dd');
            return abort(404);

       }
    }

    public function store(Request $request)
    {
        // Check permission
        $this->authorize('add', Setting::class);

        $key = implode('.', [Str::slug($request->input('group')), $request->input('key')]);
        $key_check = Setting::where('key', $key)->get()->count();

        if ($key_check > 0) {
            return back()->with('error',__('Settings :key already exists', ['key' => $key]));
        }

        $lastSetting = Setting::first();

        $request->merge(['value' => config($key)]);
        $request->merge(['key' => $key]);
        Setting::create($request->except('setting_tab'));

        request()->flashOnly('setting_tab');

        return back()->with('success', __('Settings successfully created'));
    }


    public function update(Request $request)
    {
        // Check permission
        $this->authorize('edit', Setting::class);

        $settings = Setting::all();

        foreach ($settings as $setting) {
            $content = $this->getContentBasedOnType($request, 'settings', (object) [
                'type'    => $setting->type,
                'field'   => str_replace('.', '_', $setting->key),
                'group'   => $setting->group,
            ], $setting->details);

            if ($setting->type == 'image' && $content == null) {
                continue;
            }

            if ($setting->type == 'file' && $content == null) {
                continue;
            }

            $key = preg_replace('/^'.Str::slug($setting->group).'./i', '', $setting->key);

            $setting->group = $request->input(str_replace('.', '_', $setting->key).'_group');
            $setting->key = implode('.', [Str::slug($setting->group), $key]);
            $setting->value = $content;
            $setting->save();
        }

        request()->flashOnly('setting_tab');

        return back()->with('success',__('Settings successfully saved'));
    }

    public function destroy($id)
    {
        // Check permission
        $this->authorize('delete', Setting::class);

        $setting = Setting::find($id);

        $setting->delete();

        request()->session()->flash('setting_tab', $setting->group);

        return back()->with('success',__('Settings successfully deleted'));
    }
  
}
