<?php

namespace App\Http\Controllers\Panel;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;

class SettingController extends Controller
{
    public function index(): View
    {
        Gate::authorize('settings.manage');

        $settings = Setting::orderBy('group')->get()->groupBy('group');

        return view('panel.settings.index', compact('settings'));
    }

    public function update(Request $request): RedirectResponse
    {
        Gate::authorize('settings.manage');

        foreach ($request->except('_token', '_method') as $key => $value) {
            Setting::set($key, $value);
        }

        return back()->with('success', 'Ayarlar güncellendi.');
    }
}
