<?php
// DEBUG - Upload to ajax/smsman_appdebug.php, visit in browser, then DELETE
if (session_status() === PHP_SESSION_NONE) session_start();
header('Content-Type: text/plain; charset=utf-8');

$config_path = dirname(__DIR__) . '/include/config.php';
if (!file_exists($config_path)) $config_path = dirname(__DIR__) . '/include/config.php';
include($config_path);

$sm_path = dirname(__DIR__) . '/include/SMSMan.php';
if (!file_exists($sm_path)) $sm_path = dirname(__DIR__) . '/include/SMSMan.php';
if (!class_exists('SMSMan')) require_once $sm_path;

$token    = $site_data['smsman_api_token'] ?? '';
$base_url = $site_data['smsman_api_url']   ?? 'https://api.sms-man.com/control';

$sm = new SMSMan($token, $base_url);
$r  = $sm->getApplications();

echo "success: " . ($r['success'] ? 'true' : 'false') . "\n\n";

if ($r['success']) {
    $data = $r['data'];
    echo "Total items: " . count($data) . "\n";
    echo "Is sequential array: " . (isset($data[0]) ? 'YES (index 0 exists)' : 'NO - associative/object') . "\n\n";
    
    // Show first 3 items raw
    echo "=== FIRST 3 ITEMS RAW ===\n";
    $i = 0;
    foreach ($data as $key => $val) {
        echo "Key: " . var_export($key, true) . "\n";
        echo "Value: " . json_encode($val) . "\n\n";
        if (++$i >= 3) break;
    }
} else {
    echo "Error: " . $r['error_msg'] . "\n";
}
echo "DELETE THIS FILE AFTER USE\n";