<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\SiteSetting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class SiteSettingController extends Controller
{
    /**
     * @var array<string, string>
     */
    private const KEYS = [
        'contact_whatsapp_url' => 'Contact — lien WhatsApp complet',
        'contact_email_display' => 'Contact — email affiche',
        'contact_calendly_url' => 'Contact — lien Calendly',
    ];

    public function edit(): View
    {
        $values = SiteSetting::allKeyed();
        $fields = [];
        foreach (self::KEYS as $key => $label) {
            $fields[$key] = [
                'label' => $label,
                'value' => $values[$key] ?? '',
            ];
        }

        return view('admin.settings.edit', ['fields' => $fields]);
    }

    public function update(Request $request): RedirectResponse
    {
        foreach (array_keys(self::KEYS) as $key) {
            SiteSetting::set($key, $request->input($key));
        }

        return redirect()->route('admin.settings.edit')->with('ok', 'Parametres enregistres.');
    }
}
