<?php

function is_superadmin(): bool
{
    return ($_SESSION['user']['role_id'] ?? 0) == 1;
}

function is_org_admin(): bool
{
    return ($_SESSION['user']['role_id'] ?? 0) == 2;
}

if (!function_exists('current_org_id')) {
    function current_org_id(): int
    {
        return (int)($_SESSION['org_id'] ?? 0);
    }
}

function org_condition(string $alias = ''): string
{
    if (is_superadmin()) return '';

    $orgId = current_org_id();
    if (!$orgId) return '';

    $prefix = $alias ? $alias . '.' : '';
    return " AND {$prefix}org_id = " . (int)$orgId . " ";
}
