<?php

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

/**
 * USER AUTHENTICATION CHECK
 * - Ensures user is logged in before accessing protected pages
 * - Works for both user and admin sessions
 */

function require_login() {

    // Check if user is logged in (user or admin session)
    if (!isset($_SESSION['user_id']) && !isset($_SESSION['admin_id'])) {
        header("Location: /auth/login.php");
        exit;
    }

    return true;
}

/**
 * OPTIONAL: STRICT USER-ONLY ACCESS
 * - Blocks admin accounts from user pages if needed
 */
function require_user() {

    if (!isset($_SESSION['user_id'])) {
        header("Location: /auth/login.php");
        exit;
    }

    if (isset($_SESSION['role']) && $_SESSION['role'] !== 'user') {
        header("Location: /admin/index.php");
        exit;
    }

    return true;
}