<?php
declare(strict_types=1);
require_once __DIR__ . '/functions.php';

function require_auth(): void {
    start_session();
    if (!isset($_SESSION['user'])) {
        redirect('/login.php');
    }
}

function require_guest(): void {
    start_session();
    if (isset($_SESSION['user'])) {
        $role = $_SESSION['user']['role'] ?? 'customer';
        if ($role === 'admin') {
            redirect('/admin/dashboard.php');
        } else {
            redirect('/customer/dashboard.php');
        }
    }
}

function require_admin(): void {
    require_auth();
    $role = $_SESSION['user']['role'] ?? null;
    if ($role !== 'admin') {
        http_response_code(403);
        exit;
    }
}

function require_customer(): void {
    require_auth();
    $role = $_SESSION['user']['role'] ?? null;
    if ($role !== 'customer') {
        http_response_code(403);
        exit;
    }
}

