<?php
require_once __DIR__ . '/security.php';

$tenant = enrep_tenant_segment();
if ($tenant !== '' && file_exists('../../' . $tenant . '/config.php')) {
    require_once('../../' . $tenant . '/config.php');
}
if (!defined('CONFIG_LOADED') && $tenant !== '' && file_exists('../../../' . $tenant . '/config.php')) {
    require_once('../../../' . $tenant . '/config.php');
}
if (file_exists('/config.php')) {
    require_once('/config.php');
}

if (!isset($GLOBALS['con']) || !($GLOBALS['con'] instanceof mysqli)) {
    enrep_fail(400, 'Tenant config not found');
}

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

$type = (string)($_SESSION['usertype'] ?? '');
$id = (int)($_SESSION['userid'] ?? 0);

if (($_SESSION['developer_mode'] ?? '') === 'yes') {
    echo 'a';
    exit;
}

if ($type === '' || $id < 1) {
    echo 's';
    exit;
}

switch ($type) {
    case 'admins':
        $stmt = $GLOBALS['con']->prepare("SELECT `status` FROM `admins` WHERE `id`=? LIMIT 1") or die(mysqli_error($GLOBALS['con']));
        $stmt->bind_param('i', $id);
        $stmt->execute();
        $iv = $stmt->get_result();
        $stmt->close();
        $d = mysqli_fetch_array($iv);
        echo (($d['status'] ?? '') === 'active') ? 'a' : 's';
        exit;

    case 'agents':
        $stmt = $GLOBALS['con']->prepare("SELECT `access` FROM `agents` WHERE `agent_number`=? LIMIT 1") or die(mysqli_error($GLOBALS['con']));
        $stmt->bind_param('i', $id);
        $stmt->execute();
        $iv = $stmt->get_result();
        $stmt->close();
        $d = mysqli_fetch_array($iv);
        echo ((int)($d['access'] ?? 0) === 0) ? 's' : 'a';
        exit;

    case 'surveyor':
        $stmt = $GLOBALS['con']->prepare("SELECT `access` FROM `surveyor` WHERE `id`=? LIMIT 1") or die(mysqli_error($GLOBALS['con']));
        $stmt->bind_param('i', $id);
        $stmt->execute();
        $iv = $stmt->get_result();
        $stmt->close();
        $d = mysqli_fetch_array($iv);
        echo ((int)($d['access'] ?? 0) === 0) ? 's' : 'a';
        exit;
}

echo 's';
