<?php
// includes/init.php

// Zeitzone setzen
date_default_timezone_set('Europe/Berlin');

// Error-Logger und globale Fehlerbehandlung registrieren
require_once __DIR__ . '/core/ErrorLogger.php';
set_exception_handler(['ErrorLogger', 'exceptionHandler']);
set_error_handler(['ErrorLogger', 'errorHandler']);

// Konfiguration laden
$config = include __DIR__ . '/config.php';
$scriptDir = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'] ?? ''));
$adminMarker = '/admin';
$adminLoginPath = 'login.php';
$adminBasePath = '';
$projectRootPath = '../';
$adminPos = strpos($scriptDir, $adminMarker);
if ($adminPos !== false) {
    $afterAdmin = trim(substr($scriptDir, $adminPos + strlen($adminMarker)), '/');
    $depth = $afterAdmin === '' ? 0 : substr_count($afterAdmin, '/') + 1;
    $adminLoginPath = str_repeat('../', $depth) . 'login.php';
    $adminBasePath = str_repeat('../', $depth);
    $projectRootPath = str_repeat('../', $depth + 1);
}

// Datenbank-Klasse einbinden
require_once __DIR__ . '/core/Database.php';
require_once __DIR__ . '/functions.php';
require_once __DIR__ . '/email_template_editor.php';
require_once __DIR__ . '/email_layout_editor.php';

// Autoload für Klassen
spl_autoload_register(function ($class) {
    $paths = [
        __DIR__ . '/core/',
        __DIR__ . '/../auth/',
        __DIR__ . '/../admin/',
        __DIR__ . '/menu/'
    ];
    foreach ($paths as $path) {
        $file = $path . $class . '.php';
        if (file_exists($file)) {
            require_once $file;
            return;
        }
    }
});

// Log-Aktionskonstanten laden (z. B. LOGIN_SUCCESS, SMTP_ADDED usw.)
require_once __DIR__ . '/constants/log_actions.php';
