<?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', 'enabled' => 1, 'message' => 'db_connection_error']);
}
try {
    $cols = [];
    $st = $db->query('PRAGMA table_info(app_config)');
    while ($r = $st->fetch(PDO::FETCH_ASSOC)) $cols[] = (string)($r['name'] ?? '');
    if (!in_array('emuladores_enabled', $cols, true)) $db->exec('ALTER TABLE app_config ADD COLUMN emuladores_enabled INTEGER NOT NULL DEFAULT 1');
    if (!in_array('emuladores_block_message', $cols, true)) $db->exec("ALTER TABLE app_config ADD COLUMN emuladores_block_message TEXT NOT NULL DEFAULT 'Área de emuladores bloqueada pelo painel.'");
} catch (Throwable $e) {}
try {
    $row = $db->query('SELECT emuladores_enabled, emuladores_block_message FROM app_config WHERE id=1 LIMIT 1')->fetch(PDO::FETCH_ASSOC) ?: [];
    auth_json_out([
        'status' => 'ok',
        'enabled' => (int)($row['emuladores_enabled'] ?? 1),
        'blocked' => (int)($row['emuladores_enabled'] ?? 1) !== 1,
        'message' => (string)($row['emuladores_block_message'] ?? 'Área de emuladores bloqueada pelo painel.'),
        'server_ts' => time(),
    ]);
} catch (Throwable $e) {
    auth_json_out(['status' => 'ok', 'enabled' => 1, 'blocked' => false, 'message' => '']);
}
