<?php
require __DIR__ . '/../config.php';
require FUNCTIONS_ROOT . '/functions_db.php';
require FUNCTIONS_ROOT . '/functions_wecker.php';
require FUNCTIONS_ROOT . '/functions_common.php';

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    function_json_antwort(true, ['einstellungen' => function_einstellungen_fuer_api()]);
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    function_json_antwort(false, [], 'Method not allowed');
}

$tts_modell = trim($_POST['tts_modell'] ?? '');
$lautstaerke = trim($_POST['lautstaerke'] ?? '');
$lautstaerke_musik = trim($_POST['lautstaerke_musik'] ?? '');
$musik_ducking_prozent = trim($_POST['musik_ducking_prozent'] ?? '');
$zeitzone = trim($_POST['zeitzone'] ?? '');
$tts_name = trim($_POST['tts_name'] ?? '');
$wetter_ort = trim($_POST['wetter_ort'] ?? '');
$sleep_mode_sekunden = trim($_POST['sleep_mode_sekunden'] ?? '');

if ($tts_modell !== '') {
    function_einstellung_setzen('tts_modell', $tts_modell);
}
if ($lautstaerke !== '') {
    $lautstaerke = max(0, min(100, (int)$lautstaerke));
    function_einstellung_setzen('lautstaerke', (string)$lautstaerke);
}
if ($lautstaerke_musik !== '') {
    $lautstaerke_musik = max(0, min(100, (int)$lautstaerke_musik));
    function_einstellung_setzen('lautstaerke_musik', (string)$lautstaerke_musik);
}
if ($musik_ducking_prozent !== '') {
    $musik_ducking_prozent = max(0, min(100, (int)$musik_ducking_prozent));
    function_einstellung_setzen('musik_ducking_prozent', (string)$musik_ducking_prozent);
}
if ($zeitzone !== '') {
    function_einstellung_setzen('zeitzone', $zeitzone);
}
function_einstellung_setzen('tts_name', $tts_name);
function_einstellung_setzen('wetter_ort', $wetter_ort);
if ($sleep_mode_sekunden !== '') {
    $sleep_mode_sekunden = max(0, min(86400, (int) $sleep_mode_sekunden)); // 0 = aus, max 24h
    function_einstellung_setzen('sleep_mode_sekunden', (string) $sleep_mode_sekunden);
}

function_json_antwort(true);
