<?php
session_start();

$activePage = 'User';
$activeSubPage = 'Userlist';

include("../elements/master.php");
include("../elements/functions.php");

// ----------------------------API CALL--------------------//
$jsonFilePath = __DIR__ . '/../urls/api_endpoints.json';
$jsonData = file_get_contents($jsonFilePath);
$endpoints = json_decode($jsonData, true);
$url = $endpoints['user_list_function'];
// ----------------------- ENDS HERE-------------------------//

if (!isset($_SESSION['token'])) {
    include('token_exp.php');
}

$token = $_SESSION['token'];
$result = make_get_request($url, ["Authorization: $token"]);
$response = json_decode($result, true);
$_SESSION["response"] = $response;

$excluded_pids = ['', 'admin', 'default']; // Replace with the PIDs you want to exclude

if (isset($response['items']) && is_array($response['items'])) {
    $user_list = array_map(function($user) {
        return [
            'pid' => $user['pid'] ?? '-',
            'firstname' => $user['firstname'] ?? '-',
            'lastname' => $user['lastname'] ?? '-',
            'email' => $user['email'] ?? '-',
            'access_level' => $user['access_level'] ?? '-', // Example, adjust as needed
            'category' => $user['category'] ?? '-', // Example, adjust as needed
            'cell_phone' => $user['cell_phone'] ?? '-',
            'Company' => $user['company'] ?? '-'
        ];
    }, $response['items']);

    // Filter out users with PIDs in the excludedPIDs array
    $user_list = array_filter($user_list, function($user) use ($excluded_pids) {
        return !in_array($user['pid'], $excluded_pids);
    });

    $_SESSION['userlist'] = $user_list;
} else {
    $user_list = "No users found or invalid response format.";
}
?>
