<?php
require_once __DIR__ . '/../config.php';

// Database configuration
try {
    $pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    // Log error instead of dying on live server
    error_log("Database connection failed: " . $e->getMessage());
    if (APP_ENV === 'development') {
        die("Database connection failed: " . $e->getMessage());
    } else {
        die("Database connection failed. Please check your configuration.");
    }
}

// Function to get setting value
function getSetting($key) {
    global $pdo;
    $stmt = $pdo->prepare("SELECT value FROM settings WHERE key_name = ?");
    $stmt->execute([$key]);
    return $stmt->fetchColumn();
}

// Function to update setting
function updateSetting($key, $value) {
    global $pdo;
    $stmt = $pdo->prepare("UPDATE settings SET value = ? WHERE key_name = ?");
    $stmt->execute([$value, $key]);
}
?>