<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/_app_guard.php';
app_guard_validate();
require_once dirname(__DIR__) . '/_auth_common.php';

try {
    $db = new PDO('sqlite:' . dirname(__DIR__) . '/db.db');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Throwable $e) {
    auth_json_out(['status' => 'error', 'message' => 'db_connection_error']);
}

auth_ensure_schema($db);

$mac = auth_normalize_mac((string)($_REQUEST['mac'] ?? ''));
$dnsList = auth_get_dns_list($db);
$testCfg = auth_get_test_config($db);
$appFlags = auth_get_app_flags($db);
$loginType = strtoupper((string)auth_get_active_login_mode($db));
if (!in_array($loginType, ['MAC', 'USER'], true)) {
    $loginType = ($loginType === 'NORMAL') ? 'USER' : 'MAC';
}

auth_json_out([
    'status' => 'ok',
    'ok' => true,
    'mac' => $mac,
    'dns' => $dnsList,
    'login_type' => $loginType,
    'test_enabled' => (int)($testCfg['enabled'] ?? 0),
    'auto_mac_enabled' => (int)($testCfg['auto_mac_enabled'] ?? 0),
    'test_button_enabled' => (int)($appFlags['test_button_enabled'] ?? 1),
]);
