<?php 
// strictly enforce types
declare(strict_types=1);

// --- INSTALLER INTERCEPT ---
// If the .env file does not exist, the app is not installed. Route to the installer inside public/
if (!file_exists(dirname(__DIR__) . '/.env')) {
    header("Location: installer/index.php");
    exit;
}

// --- HTTP SECURITY HEADERS ---
header("X-Frame-Options: SAMEORIGIN"); // Prevents Clickjacking
header("X-XSS-Protection: 1; mode=block"); // Cross-Site Scripting filter
header("X-Content-Type-Options: nosniff"); // Prevents MIME-type sniffing
header("Referrer-Policy: strict-origin-when-cross-origin");
header("Strict-Transport-Security: max-age=31536000; includeSubDomains; preload"); // Force HTTPS

// --- ZERO WHITE SCREEN: GLOBAL ERROR HANDLER ---
require_once dirname(__DIR__) . '/app/Core/ErrorHandler.php';
ErrorHandler::register();

// 1. Require Composer Autoloader
$composerAutoload = dirname(__DIR__) . '/vendor/autoload.php';
if (file_exists($composerAutoload)) {
    require_once $composerAutoload;
}

// 2. Require Configuration FIRST
require_once '../config/config.php';

// ==========================================
// CHRISTODEV LICENSE ENGINE
require_once 'license_check.php';
// ==========================================

// 3. Require remaining core files & helpers
require_once '../app/Helpers/session_helper.php';
require_once '../app/Helpers/settings_helper.php';
require_once '../app/Helpers/flash_helper.php';
require_once '../app/Helpers/log_helper.php';
require_once '../app/Helpers/totp_helper.php';

// NEW: 2FA Engine
$mailHelperPath = '../app/Helpers/mail_helper.php';
if (file_exists($mailHelperPath)) {
    require_once $mailHelperPath;
}

require_once '../app/Core/App.php';
require_once '../app/Core/Controller.php';
require_once '../app/Core/Database.php';

// Boot up the application engine
$app = new App();