<?php
/**
 * Simple key/value site settings helper using `site_settings` table
 */
function get_setting($pdo, $key, $default = null) {
    try {
        $stmt = $pdo->prepare("SELECT value FROM site_settings WHERE `key` = ? LIMIT 1");
        $stmt->execute([$key]);
        $row = $stmt->fetch();
        if ($row) return $row['value'];
    } catch (Exception $e) {
        // ignore
    }
    return $default;
}

function set_setting($pdo, $key, $value) {
    try {
        $stmt = $pdo->prepare("INSERT INTO site_settings (`key`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), updated_at = CURRENT_TIMESTAMP");
        return $stmt->execute([$key, $value]);
    } catch (Exception $e) {
        return false;
    }
}

function get_all_settings($pdo) {
    try {
        $stmt = $pdo->query("SELECT `key`, `value` FROM site_settings");
        $rows = $stmt->fetchAll();
        $out = [];
        foreach ($rows as $r) $out[$r['key']] = $r['value'];
        return $out;
    } catch (Exception $e) {
        return [];
    }
}

// Function to get system name for branding
function get_system_name($pdo) {
    return get_setting($pdo, 'system_name', 'স্কুল ম্যানেজমেন্ট সিস্টেম');
}

// Function to get English system name for branding
function get_system_name_en($pdo) {
    return get_setting($pdo, 'system_name_en', 'School Management System');
}