<?php

namespace App\Modules\Pilkades\Http\Controllers\SuperAdmin;

use App\Http\Controllers\Controller;
use App\Modules\Pilkades\Models\Setting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class SettingController extends Controller
{
    public function index(): View
    {
        $settings = Setting::whereNull('desa_id')->pluck('value', 'key');

        return view('pilkades::super-admin.settings.index', compact('settings'));
    }

    public function update(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'site_name' => ['nullable', 'string', 'max:255'],
            'seo_default_title' => ['nullable', 'string', 'max:255'],
            'seo_default_description' => ['nullable', 'string', 'max:500'],
            'contact_email' => ['nullable', 'email'],
        ]);

        foreach ($data as $key => $value) {
            Setting::updateOrCreate(
                ['desa_id' => null, 'key' => $key],
                ['value' => $value],
            );
        }

        return back()->with('success', 'Pengaturan berhasil disimpan.');
    }
}
