<?php
ob_start(); // Start output buffering
session_start();
require_once __DIR__ . '/config/app.php';

// Redirect to login if the session is expired
if (!isset($_SESSION['user'])) {
    header("location:" . APP_URL . "login.php");
    exit;
}

// Get the current page name
$currentPage = basename($_SERVER['PHP_SELF']);

// Restrict `investor_share` users to only `index_share.php`
if (isset($_SESSION['investor_access']) && $_SESSION['investor_access'] === true) {
    // If the current page is NOT `index_share.php`, redirect to it
    if ($currentPage !== 'index_share.php') {
        header("location:" . APP_URL . "index_share.php");
        exit;
    }
}

// Restrict `attendance_access` users to only `index_attendance.php`
if (isset($_SESSION['attendance_access']) && $_SESSION['attendance_access'] === true) {
    // If the current page is NOT `index_attendance.php`, redirect to it
    if ($currentPage !== 'index_attendance.php') {
        header("location:" . APP_URL . "index_attendance.php");
        exit;
    }
}

// For general users, ensure they cannot access `index_share.php` or `index_attendance.php`
if (!isset($_SESSION['investor_access']) && !isset($_SESSION['attendance_access'])) {
    // Redirect general users away from restricted pages
    if ($currentPage === 'index_share.php' || $currentPage === 'index_attendance.php') {
        header("location:" . APP_URL . "index.php");
        exit;
    }
}

ob_end_flush(); // Flush the output buffer
?>
