<?php
require_once __DIR__ . '/../app/bootstrap.php';
use App\Core\Auth;use App\Core\Database;use App\Core\Response;use App\Core\Security;
Auth::requireAdminModule('settings');
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !verify_csrf()) Response::json(['ok'=>false,'message'=>'Invalid request.'],419);
$data=request_data();$key=trim((string)($data['setting_key']??''));
if(!preg_match('/^[a-z0-9_.-]{2,150}$/i',$key))Response::json(['ok'=>false,'message'=>'Invalid setting key.'],422);
$value=mb_substr((string)($data['setting_value']??''),0,10000);$group=mb_substr(trim((string)($data['setting_group']??'general')),0,80);
Database::execute('INSERT INTO settings (setting_key,setting_value,setting_group,updated_at) VALUES (?,?,?,NOW()) ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value),setting_group=VALUES(setting_group),updated_at=NOW()',[$key,$value,$group]);
Security::logActivity(Auth::id(),'setting_updated',['key'=>$key,'group'=>$group]);
Response::json(['ok'=>true,'message'=>'Setting saved.']);
