<?php
// app/Core/init.php

// Load configuration FIRST (before starting session)
require_once __DIR__ . '/../../config/config.php';

// Now start session with SESSION_LIFETIME available
if (session_status() === PHP_SESSION_NONE) {
    session_start([
        'cookie_httponly' => true,
        'cookie_secure' => isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on',
        'cookie_samesite' => 'Strict',
        'use_strict_mode' => true,
        'cookie_lifetime' => 0,
        'gc_maxlifetime' => defined('SESSION_LIFETIME') ? SESSION_LIFETIME : 1800
    ]);
}


// Load core classes
require_once __DIR__ . '/Database.php';
require_once __DIR__ . '/Auth.php';
require_once __DIR__ . '/Security.php';
require_once __DIR__ . '/Mailer.php';
require_once __DIR__ . '/Upload.php';


// Load helpers
require_once __DIR__ . '/../Helpers/general.php';
require_once __DIR__ . '/../Helpers/validation.php';
require_once __DIR__ . '/../Helpers/format.php';
require_once __DIR__ . '/../Helpers/file.php';



// Payment Gateway Classes
require_once __DIR__ . '/../Payment/PaymentInterface.php';
require_once __DIR__ . '/../Payment/Paystack.php';
require_once __DIR__ . '/../Payment/Flutterwave.php';
require_once __DIR__ . '/../Payment/PaymentGatewayFactory.php';

// Autoload classes
spl_autoload_register(function ($class) {
    $paths = [
        __DIR__ . '/../Models/' . $class . '.php',
        __DIR__ . '/../Services/' . $class . '.php',
        __DIR__ . '/../Payment/' . $class . '.php',
    ];
    
    foreach ($paths as $path) {
        if (file_exists($path)) {
            require_once $path;
            return;
        }
    }
});

// Error handling
if (defined('APP_DEBUG') && APP_DEBUG) {
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
} else {
    error_reporting(0);
    ini_set('display_errors', 0);
    ini_set('log_errors', 1);
    
    // Ensure log directory exists
    $log_dir = STORAGE_PATH . '/logs';
    if (!file_exists($log_dir)) {
        mkdir($log_dir, 0755, true);
    }
    ini_set('error_log', $log_dir . '/error.log');
}

// Security headers
header('X-Content-Type-Options: nosniff');
header('X-Frame-Options: SAMEORIGIN');
header('X-XSS-Protection: 1; mode=block');
header('Referrer-Policy: strict-origin-when-cross-origin');

// Session regeneration
if (!isset($_SESSION['last_regeneration'])) {
    $_SESSION['last_regeneration'] = time();
    session_regenerate_id(true);
} elseif (time() - $_SESSION['last_regeneration'] > 300) {
    $_SESSION['last_regeneration'] = time();
    session_regenerate_id(true);
}

// Initialize database
try {
    $db = Database::getInstance();
} catch (Exception $e) {
    if (defined('APP_DEBUG') && APP_DEBUG) {
        die("Database Error: " . $e->getMessage());
    } else {
        // Ensure log directory exists before logging
        $log_dir = STORAGE_PATH . '/logs';
        if (!file_exists($log_dir)) {
            mkdir($log_dir, 0755, true);
        }
        error_log("Database Error: " . $e->getMessage());
        die("Technical difficulties. Please try again later.");
    }
}

// Set encoding
if (function_exists('mb_internal_encoding')) {
    mb_internal_encoding('UTF-8');
}
if (function_exists('mb_http_output')) {
    mb_http_output('UTF-8');
}

// Check session timeout
if (class_exists('Auth')) {
    Auth::checkTimeout();
}