<?php

function current_company_id() {
    return $_SESSION['company_id'] ?? null;
}

function is_super_admin() {
    return !empty($_SESSION['is_super_admin']);
}

function requireTenant() {
    if (is_super_admin()) return;

    if (!current_company_id()) {
        http_response_code(403);
        exit('Tenant context missing.');
    }
}

function tenantWhere($alias = '') {
    if (is_super_admin()) return '';

    $prefix = $alias ? $alias . '.' : '';
    return " AND {$prefix}company_id = :company_id ";
}

function tenantParams($params = []) {
    if (is_super_admin()) return $params;

    $params['company_id'] = current_company_id();
    return $params;
}