<?php

// use App\Models\State;
// use App\Models\Estate;
// use App\Models\User; // Assuming you have a User model
// use App\Models\Department;
// use App\Models\ApprovalRequest;
use Core\Auth;
// use Core\Helpers;
use App\Models\Notification;
use Core\Request;



use function Core\Helpers\formatCurrency;
use function Core\Helpers\excerpt;
use function Core\Helpers\toInt;

// use Core\Helpers\NotificationHelper;

// // Initialize data variables
// $states = [];
// $estates = [];
// $users = [];
// $notifications = [];
$user = [];

// $pendingUpdateAproval = 0;

$formatCurrency = function($amount, $currency = '₦') {
    return formatCurrency($amount, $currency);
};



$notifications = [];
if (Auth::check()) {
    $user = Auth::user();

    // // Staff and Managers can only view data from their assigned state.
    // if (in_array($user['role'], ['staff', 'manager'])) {
    //     $states = State::whereAll('id', $user['state_id']);
    //     $estates = Estate::whereAll('state_id', $user['state_id']);
    //     $users = User::whereAll('state_id', $user['state_id']);

    // } else {
    //     // Other roles (e.g., 'super_admin', 'director') can view all data.
    //     $states = State::all();
    //     $estates = Estate::all();
    //     $users = User::all();
    // }


    // if($user['role'] == 'super_admin') {
    //     $pendingUpdateAproval = ApprovalRequest::countPending();
    // }else if($user['role'] == 'manager') {
    //     $pendingUpdateAproval = ApprovalRequest::countPendingByState($user['state_id']);
    // }



    // $notifications = NotificationHelper::getForUser($user['id']);

        // Notifications - Sorted Newest First
        // $notifications = Notification::whereAll('user_id', $user['id']); 
        // if (!empty($notifications)) {
        //     usort($notifications, fn($a, $b) => strtotime($b['created_at']) - strtotime($a['created_at']));
        // }

}

// $getState = function($id, $column = 'name'){
//     return State::getById($id, $column);
// };

// $getDepartment = function($id, $column = 'name'){
//     return Department::getById($id, $column);
// };




// // Return the data as a single associative array

// // echo $users['first_name'];
// return [
//     "states" => $states,
//     "estates" => $estates,
//     "users" => $users,
//     "notifications"=> $notifications,
//     "user" => $user,
//     "countPendingApprovals" => $pendingUpdateAproval,
//     "getState"=> $getState,
//     "getDepartment"=> $getDepartment,
//     "activeMenu"=> '',
//     'app_url'=> APP_URL,
// ];

// print_r($user);



return [
    "user" => $user,
    "formatCurrency" => $formatCurrency,
    "excerpt" => function($text, $length = 20) {
        return excerpt($text, $length);
    },

    "global_message" => Request::get('message', null),
    "global_status" => Request::get('status', null), // default to '

    "toInt" => function($value) {
        return toInt($value);
    },

    'notifications'=> $notifications,
    
    "parseRoomData" => function($input) {
        if (empty($input)) return [];
        
        // Decode HTML entities (twice to be safe against double encoding like &quot;)
        $clean = html_entity_decode(html_entity_decode($input));
        
        $data = json_decode($clean, true);
        
        // If JSON decode fails or isn't an array, try comma fallback
        if (json_last_error() !== JSON_ERROR_NONE || !is_array($data)) {
            // Check if it's just a comma separated string
            if (strpos($input, '[') === false && !empty($input)) {
                return array_map(function($t) { 
                    return ['icon' => 'bi-check-circle', 'text' => trim($t)]; 
                }, explode(',', $input));
            }
            return [];
        }
        return $data;
    },

];