<?php

namespace App\Http\Controllers;

use App\Http\Requests\UpdateSettingsRequest;
use App\Services\AuditLogService;
use App\Services\SettingsService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class SettingsController extends Controller
{
    public function index(SettingsService $settings): View
    {
        return view('settings.index', [
            'settings' => $settings->allGrouped(),
        ]);
    }

    public function update(
        UpdateSettingsRequest $request,
        SettingsService $settings,
        AuditLogService $auditLog,
    ): RedirectResponse {
        $settings->update($request->validated('settings'));

        $auditLog->record('settings', 'settings.updated', 'Control center settings were updated.', null, $request->user());

        return back()->with('status', 'Settings saved.');
    }
}
