<?php
/**==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==**/
/**
 * 客户端指纹信息风险评估处理器
 * 接收前端发送的指纹数据并进行综合风险判断
 */
require_once __DIR__ . '/cong.php';
require_once __DIR__ . '/lib/CloakHttpHelpers.php';
require_once __DIR__ . '/lib/Cloak/CloakSession.php';

CloakFcheckCors::handlePreflight();
CloakSession::start();

header('Content-Type: application/json; charset=utf-8');
CloakFcheckCors::emitHeaders();

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode([
        'status'  => 'error',
        'message' => '只允许POST请求',
    ]);
    exit;
}

$hdata = $_POST['hdata'] ?? '';
if ($hdata === '') {
    http_response_code(400);
    echo json_encode([
        'status'  => 'error',
        'message' => '无效的指纹数据',
    ]);
    exit;
}

require_once __DIR__ . '/lib/DbHelper.php';
require_once __DIR__ . '/lib/bootstrap.php';
require_once __DIR__ . '/config/ConfigLoader.php';
require_once __DIR__ . '/lib/Cloak/FCheckHandler.php';

$result = FCheckHandler::processFromHdata($hdata);
if (empty($result['ok'])) {
    $status = (int) ($result['status'] ?? 400);
    http_response_code($status);
    echo json_encode([
        'status'  => 'error',
        'message' => $result['message'] ?? '处理失败',
    ]);
    exit;
}

unset($result['ok'], $result['status'], $result['fingerprint_info'], $result['api_request'], $result['api_response']);
echo json_encode($result);
