<?php
declare(strict_types=1);

ini_set('display_errors', '0');
require_once __DIR__ . '/../auth.php';
require_once __DIR__ . '/../db_bootstrap.php';
require_once __DIR__ . '/api_common.php';
require_once __DIR__ . '/../settings_store.php';
require_once __DIR__ . '/../sms_provider.php';

api_require_login();
if (function_exists('require_perm')) require_perm('can_settings');

header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');

try {
    $conn = ensure_db(true);
    $cfg = sms_provider_normalize_config(setting_get($conn, 'sms_provider', []));
    $result = sms_provider_detect_local_modem($cfg);
    api_json_out($result, 200, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} catch (Throwable $e) {
    api_json_out([
        'ok'=>false,
        'verified'=>false,
        'ports'=>[],
        'message'=>'تعذر تشغيل الاكتشاف التلقائي لمودم الرسائل: ' . $e->getMessage(),
    ], 500, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
