<?php
// Enable error reporting for debugging
// Production error reporting - disabled for security
error_reporting(0);
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/error.log');

// Set time limit for long operations
set_time_limit(300);

// Enable output buffering to catch any unexpected output
ob_start();

// Custom error handler for fatal errors
function customErrorHandler($errno, $errstr, $errfile, $errline) {
    $error_msg = "Error [$errno]: $errstr in $errfile on line $errline";
    error_log($error_msg);
    
    if ($GLOBALS['config']['debug_mode']) {
        echo "<div style='background: #ffebee; border: 1px solid #f44336; padding: 10px; margin: 10px; border-radius: 4px;'>";
        echo "<h3 style='color: #d32f2f; margin: 0 0 10px 0;'>PHP Error</h3>";
        echo "<p><strong>Error:</strong> " . htmlspecialchars($errstr) . "</p>";
        echo "<p><strong>File:</strong> " . htmlspecialchars($errfile) . "</p>";
        echo "<p><strong>Line:</strong> " . htmlspecialchars($errline) . "</p>";
        echo "</div>";
    }
    
    return true; // Don't execute PHP internal error handler
}

// Custom fatal error handler
function customFatalErrorHandler() {
    $error = error_get_last();
    if ($error !== null && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
        $error_msg = "Fatal Error: {$error['message']} in {$error['file']} on line {$error['line']}";
        error_log($error_msg);
        
        if ($GLOBALS['config']['debug_mode']) {
            echo "<div style='background: #ffebee; border: 1px solid #f44336; padding: 10px; margin: 10px; border-radius: 4px;'>";
            echo "<h3 style='color: #d32f2f; margin: 0 0 10px 0;'>Fatal Error</h3>";
            echo "<p><strong>Error:</strong> " . htmlspecialchars($error['message']) . "</p>";
            echo "<p><strong>File:</strong> " . htmlspecialchars($error['file']) . "</p>";
            echo "<p><strong>Line:</strong> " . htmlspecialchars($error['line']) . "</p>";
            echo "</div>";
        }
    }
}

// Set error handlers
set_error_handler('customErrorHandler');
register_shutdown_function('customFatalErrorHandler');

$config = [
    'timezone' => '+00:00', // UTC/GMT
    'debug_mode' => false, // Set to false in production
    'store_registration_key' => 'd-bundles-2025-secret-key', // Secret key to protect store registration
];


$host = 'localhost'; // Database host
$user = 'richiviy_dbundles'; // Database username
$password = 'Uv&s@gO)J+9)'; // Database password
$dbname = 'richiviy_dbundles'; // Database name
// Database connection with better error handling
$conn = mysqli_connect($host, $user, $password, $dbname);

if (!$conn) {
    $error_msg = "Database connection failed: " . mysqli_connect_error();
    error_log($error_msg);
    
    if ($config['debug_mode']) {
        die("<h2>Database Connection Error</h2><p>" . htmlspecialchars($error_msg) . "</p>");
    } else {
        die("Database connection failed. Please check your configuration.");
    }
}

// Set charset to UTF-8
if (!mysqli_set_charset($conn, "utf8")) {
    $error_msg = "Error setting charset: " . mysqli_error($conn);
    error_log($error_msg);
    
    if ($config['debug_mode']) {
        echo "<p>Warning: " . htmlspecialchars($error_msg) . "</p>";
    }
}

// Apply timezone globally (PHP)
$tzOffset = $config['timezone'] ?? '+00:00';
// Map offset to a PHP timezone identifier (UTC for +00:00)
if ($tzOffset === '+00:00' || strtoupper($tzOffset) === 'UTC' || strtoupper($tzOffset) === 'GMT') {
    date_default_timezone_set('UTC');
} else {
    // Fallback to UTC if unknown; you can map other offsets as needed
    date_default_timezone_set('UTC');
}

// Apply timezone for the current MySQL session
@mysqli_query($conn, "SET time_zone = '" . mysqli_real_escape_string($conn, $tzOffset) . "'");
?>