<?php
/**
 * Auth helper functions — call at the top of every protected page.
 */

function requireLogin(): void
{
    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }
    if (empty($_SESSION['user_id'])) {
        header('Location: login.php');
        exit;
    }
}

/** Admin-only pages: redirect technicians to their dashboard. */
function requireAdmin(): void
{
    requireLogin();
    if (($_SESSION['role'] ?? '') !== 'admin') {
        header('Location: tech_dashboard.php');
        exit;
    }
}

/** Technician-only pages: redirect admins to admin dashboard. */
function requireTechnician(): void
{
    requireLogin();
    if (($_SESSION['role'] ?? '') !== 'technician') {
        header('Location: dashboard.php');
        exit;
    }
}

function isAdmin(): bool
{
    return isset($_SESSION['role']) && $_SESSION['role'] === 'admin';
}

function isTechnician(): bool
{
    return isset($_SESSION['role']) && $_SESSION['role'] === 'technician';
}
