<?php
/**
 * Heritage Voice — app_settings.php
 * GET  /api/app-settings        Lấy tất cả settings (public)
 * PUT  /api/app-settings        Cập nhật settings (admin only)
 */
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/db.php';
require_once __DIR__ . '/helpers.php';

function app_settings_get() {
    $rows = db()->query("SELECT `key`, `value` FROM hv_app_settings")->fetchAll();
    $data = [];
    foreach ($rows as $r) $data[$r['key']] = $r['value'];
    json_ok($data);
}

function app_settings_update() {
    require_admin();
    $b = body();
    foreach ($b as $key => $value) {
        db()->prepare("INSERT INTO hv_app_settings (`key`, `value`) VALUES (:k, :v)
            ON DUPLICATE KEY UPDATE `value` = :v2, updated_at = NOW()")
            ->execute([':k' => $key, ':v' => $value, ':v2' => $value]);
    }
    json_ok(null, 'Đã cập nhật settings');
}
