<?php

namespace App\Http\Controllers;

use App\Models\Setting;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class SettingsController extends Controller
{
    private const MACRO_KEYS = [
        'macro_name',
        'macro_uuid',
        'macro_collection_name',
        'macro_collection_uuid',
        'agenda_start_title',
        'agenda_end_title',
        'agenda_announcement_position',
        'agenda_sermon_matching',
    ];

    public function index(): Response
    {
        $settings = [];
        foreach (self::MACRO_KEYS as $key) {
            $settings[$key] = Setting::get($key);
        }

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

    public function update(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'key' => ['required', 'string', 'in:'.implode(',', self::MACRO_KEYS)],
            'value' => ['nullable', 'string', 'max:500'],
        ]);

        Setting::set($validated['key'], $validated['value']);

        return response()->json(['success' => true]);
    }
}
