<?php
/**
 * Main Layout File
 * Kenyan Pension & Life Insurance Management System
 * 
 * IMPORTANT: Authentication checks must happen BEFORE any HTML output
 */

// =============================================================================
// USE STATEMENTS MUST BE AT THE TOP (not inside if blocks)
// =============================================================================
// Uncomment this if you're using SCSS compiler
// use ScssPhp\ScssPhp\Compiler;

// =============================================================================
// 1. START SESSION (Before any output)
// =============================================================================
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// =============================================================================
// 2. INCLUDE REQUIRED FILES (Before any output)
// =============================================================================
require_once 'layouts/dbconn.php';
require_once 'includes/auth.php';

// =============================================================================
// 3. AUTHENTICATION CHECK (Before any output)
// =============================================================================
// Uncomment this line to require login for all pages
// requireLogin();

// OR check if user is logged in and redirect if not
if (!isLoggedIn()) {
    header('Location: login.php');
    exit();
}

// Optional: Check session timeout
checkSessionTimeout(120); // 120 minutes timeout

// =============================================================================
// 4. HELPER FUNCTIONS
// =============================================================================
function includeFileWithVariables($filePath, $variables = array(), $print = true)
{
    $output = NULL;
    if(file_exists($filePath)){
        // Extract the variables to a local namespace
        extract($variables);
        // Start output buffering
        ob_start();
        // Include the template file
        include $filePath;
        // End buffering and return its contents
        $output = ob_get_clean();
    }
    if ($print) {
        print $output;
    }
    return $output;
}

// =============================================================================
// 5. SCSS COMPILATION (Optional)
// =============================================================================
$isScssconverted = false;

if($isScssconverted){
    require_once ("scssphp/scss.inc.php");
    
    // FIXED: Use fully qualified class name instead of 'use' statement
    $compiler = new \ScssPhp\ScssPhp\Compiler();
    
    $compine_css = "assets/css/app.min.css";
    $source_scss = "assets/scss/config/interactive/app.scss";
    
    if (file_exists($source_scss)) {
        $scssContents = file_get_contents($source_scss);
        $import_path = "assets/scss/config/interactive";
        $compiler->addImportPath($import_path);
        $target_css = $compine_css;
        
        try {
            $css = $compiler->compile($scssContents);
            if (!empty($css) && is_string($css)) {
                file_put_contents($target_css, $css);
            }
        } catch (Exception $e) {
            error_log("SCSS Compilation Error: " . $e->getMessage());
        }
    }
}

// =============================================================================
// 6. NOW OUTPUT CAN BEGIN
// =============================================================================
?>
<!doctype html>
<html lang="en" 
      data-layout="vertical" 
      data-layout-style="detached" 
      data-sidebar="light" 
      data-topbar="dark" 
      data-sidebar-size="lg" 
      data-sidebar-image="none" 
      data-preloader="disable">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kenyan Pension & Life Insurance System</title>
    
    <!-- CSS Files -->
    <link href="assets/css/app.min.css" rel="stylesheet" type="text/css">
    
    <!-- Additional head content can go here -->
</head>
<body>
    <!-- Your page content here -->
    
    <!-- Display user info (optional) -->
    <div class="user-info">
        Welcome, <?php echo htmlspecialchars(getUserFullName()); ?>
        (<?php echo htmlspecialchars(getUserRole()); ?>)
    </div>
    
    <!-- JavaScript Files -->
    <script src="assets/js/app.min.js"></script>
</body>
</html>