<?php
/// file: app/Controllers/Admin/SettingsController.php

declare(strict_types=1);

namespace App\Controllers\Admin;

use App\Core\Controller;
use App\Core\Csrf;
use App\Core\Request;
use App\Core\Response;
use App\Models\SiteSetting;

final class SettingsController extends Controller
{
    public function index(Request $request): Response
    {
        $settings = SiteSetting::allAssoc();
        $html = $this->adminView('admin/settings/index', ['settings' => $settings]);
        return Response::html($html);
    }

    public function save(Request $request): Response
    {
        if (! Csrf::verify($request->input('_csrf'))) {
            return $this->redirect(url('/admin/settings'));
        }
        $keys = ['brand_name', 'tagline', 'contact_email', 'social_instagram', 'social_linkedin', 'og_default_image', 'default_placeholder_image'];
        foreach ($keys as $k) {
            SiteSetting::set($k, trim((string) $request->input($k, '')));
        }
        return $this->redirect(url('/admin/settings'));
    }
}
