<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AuditLog;
use App\Models\SiteSetting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class SettingsController extends Controller
{
    public function edit(): Response
    {
        return Inertia::render('Admin/Settings/Edit', [
            'settings' => SiteSetting::allSettings(),
        ]);
    }

    public function update(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'title' => ['required', 'string', 'max:100'],
            'logo' => ['required', 'string', 'max:255'],
            'favicon' => ['required', 'string', 'max:255'],
            'donate_cost' => ['required', 'integer', 'min:1'],
        ]);

        foreach ($data as $key => $value) {
            SiteSetting::query()->updateOrCreate(['key' => $key], ['value' => $value]);
        }

        AuditLog::record('settings.updated', null, $data);

        return back()->with('success', 'Settings updated.');
    }
}
