<?php
// Enable detailed error reporting for debugging 500 errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$section = 'dashboard';
require_once __DIR__ . '/db/config.php';

// Try to connect to DB first to catch connection errors early
try {
    $db = db();
} catch (PDOException $e) {
    die("Database Connection Error: " . $e->getMessage());
} catch (Exception $e) {
    die("General Error: " . $e->getMessage());
}

// Now include helpers, which can use the existing $db connection
require_once __DIR__ . '/helpers.php';

// Auth Check
require_once __DIR__ . '/includes/auth.php';
check_auth();

// $db is already set above, so no need to call db() again, but it's safe if we do.
// $db = db(); 

$lang = $_SESSION['lang'];

require_once __DIR__ . '/includes/actions.php';
require_once __DIR__ . '/includes/common_data.php';

if (!isset($_GET['ajax_search'])) {
    require_once __DIR__ . '/includes/layout/header.php';
}

require_once __DIR__ . '/includes/pages/dashboard.php';

if (!isset($_GET['ajax_search'])) {
    require_once __DIR__ . '/includes/layout/footer.php';
}
