<?php

namespace App\Http\Controllers\Settings;

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

class SettingController extends Controller
{
    /**
     * Show the general settings page.
     */
    public function edit(Request $request): Response
    {
        $settings = Setting::getSettings();

        return Inertia::render('admin/settings/General', [
            'settings' => $settings,
        ]);
    }

    /**
     * Update the system information.
     */
    public function update(Request $request): RedirectResponse
    {
        $request->validate([
            'site_name'        => 'required|string|max:255',
            'site_title'       => 'nullable|string|max:255',
            'site_description' => 'nullable|string|max:1000',
            'site_keywords'    => 'nullable|string|max:500',
            'admin_email'      => 'nullable|email|max:255',
            'phone'            => 'nullable|string|max:20',
            'fax'              => 'nullable|string|max:20',
            'gtm_code'         => 'nullable|string',
            'favicon'          => 'nullable|string',
            'logo'             => 'nullable|string',
        ]);

        $settings = Setting::getSettings();
        $data = $request->only([
            'site_name',
            'site_title',
            'site_description',
            'site_keywords',
            'admin_email',
            'phone',
            'fax',
            'gtm_code',
            'favicon',
            'logo',
        ]);

        $settings->updateSettings($data);

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