<?php
require_once dirname(__DIR__) . '/config/database.php';
session_start();
define('ADMIN_BASE', dirname($_SERVER['SCRIPT_NAME']));
$baseUrl = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\');
if (strpos($baseUrl, '\\') !== false) $baseUrl = str_replace('\\', '/', $baseUrl);
define('ADMIN_SITE_ROOT', preg_replace('#/admin.*$#', '', $baseUrl));
define('UPLOAD_PATH', dirname(__DIR__) . '/uploads/');
define('UPLOAD_URL', 'uploads/');

function isAdminLoggedIn() {
    return !empty($_SESSION['admin_id']);
}

function getSetting($key, $default = '') {
    $pdo = getDB();
    $stmt = $pdo->prepare("SELECT setting_value FROM site_settings WHERE setting_key = ?");
    $stmt->execute([$key]);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    return $row ? $row['setting_value'] : $default;
}

function requireAdmin() {
    if (!isAdminLoggedIn()) {
        header('Location: login.php');
        exit;
    }
}
