<?php
/**
 * 子进程：测试 RouteResolver 二次风控分支
 * 输出 JSON：{risk_page, has_ip, has_log_id, has_f_check, visit_to_3_init, output_len, error}
 */
$root = dirname(__DIR__);
$fixture = $root . '/tools/fixtures/regression_risk.php';

$_SERVER['PHP_SELF']             = '/index.php';
$_SERVER['HTTP_HOST']            = 'risk.test.local';
$_SERVER['REMOTE_ADDR']          = '203.0.113.10';
$_SERVER['HTTP_USER_AGENT']      = 'Mozilla/5.0 (Windows NT 10.0)';
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US';
$_SERVER['SERVER_PORT']          = 443;
$_SERVER['HTTPS']                = 'on';

require_once $root . '/cong.php';
session_start();
$_SESSION = ['check_result' => 'true'];

include $fixture;

$GLOBALS['__risk_log_captured'] = null;
if (!function_exists('write_log_db')) {
    function write_log_db($logs)
    {
        $GLOBALS['__risk_log_captured'] = $logs;
        return 424242;
    }
}

require_once $root . '/lib/bootstrap.php';

$v_info         = new visitorInfo();
$v_info->v_ip   = '203.0.113.10';
$v_info->v_country = 'US';

$logs = [
    'site_name'        => 'risk.test.local',
    'customers_ip'     => $v_info->v_ip,
    'country'          => 'US',
    'v_Browser'        => 'Chrome',
    'v_referer'        => 'https://www.google.com/',
    'Client'           => 'PC',
    'v_PageURL'        => '/index.php',
    'accept_language'  => 'en-US',
    'visit_md5_code'   => 'reg_risk_md5',
    'campagin_id'      => COSTM_IP_SCORE,
    'result'           => 'true',
    'device'           => 'PC',
    'reason'           => '',
];

$visitTo3Before = array_key_exists('visit_to_3', $_SESSION) ? $_SESSION['visit_to_3'] : null;

ob_start();
RouteResolver::dispatch([
    'v_info'          => $v_info,
    'logs'            => $logs,
    'config_name'     => 'index',
    'reason'          => '',
    'model'           => 'PC',
]);
$out = ob_get_clean();

echo json_encode([
    'risk_page'        => strpos($out, 'Powered By Google') !== false,
    'has_ip'           => strpos($out, '203.0.113.10') !== false,
    'has_log_id'       => strpos($out, '424242') !== false,
    'has_f_check'      => strpos($out, 'f_check.php') !== false,
    'webgl_null_guard' => strpos($out, 'dbgExt && typeof') !== false,
    'visit_to_3_init'  => $_SESSION['visit_to_3'] ?? null,
    'visit_to_3_was_unset' => $visitTo3Before === null,
    'log_result'       => $GLOBALS['__risk_log_captured']['result'] ?? null,
    'log_reason'       => $GLOBALS['__risk_log_captured']['reason'] ?? null,
    'log_fp_url'       => $GLOBALS['__risk_log_captured']['fp_url'] ?? null,
    'output_len'       => strlen($out),
    'has_fatal'        => stripos($out, 'Fatal error') !== false,
], JSON_UNESCAPED_UNICODE);
