<?php
/**
 * Direct, isolated verification of generateGeneralResponse()'s phase-
 * aware balance fix in api/colex/chat.php.
 *
 * IMPORTANT FINDING made while trying to verify this live: tracing the
 * real dispatch logic (chat.php lines 259-273) shows generateGeneralResponse()
 * is NOT reachable through any real user interaction, in either the LLM-
 * configured or no-API-key fallback path — detectIntent()'s 'general'
 * result is intercepted and answered directly at line 261-265 BEFORE
 * generateResponse() (the only caller of generateGeneralResponse()) is
 * ever invoked, and generateResponse()'s own switch has no other path
 * to its default case, since detectIntent() can only ever return one of
 * its 6 named intents or 'general'. Confirmed via a real HTTP round trip
 * (a general-sounding message returned the canned "reasoning engine
 * isn't connected" text, not generateGeneralResponse()'s output) before
 * concluding this, not assumed from reading alone. This function is real,
 * dead-but-present code — the same "additive, confirmed unreachable, left
 * in place" pattern this codebase already uses elsewhere (see CLAUDE.md).
 * The fix inside it is still correct and worth keeping (matches the
 * additive-only convention), so verified here by direct function call
 * instead of an HTTP round trip.
 */
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../src/JourneySegmentation.php';
require_once __DIR__ . '/../src/PropFirmRuleEngine.php';
require_once __DIR__ . '/../api/analytics/engine.php';

// Load just the function definitions from chat.php without triggering its
// top-level request-handling logic — REQUEST_METHOD is neither GET nor
// POST, so both top-level branches are skipped and the file simply
// finishes after defining every function.
$auth = new class {
    public function isLoggedIn() { return true; }
    public function getUserId() { return 'zz-test'; }
};
$db = new class {
    public function raw($sql, $params = []) { return []; }
};
$_SERVER['REQUEST_METHOD'] = 'CLI_ISOLATED_TEST';
require __DIR__ . '/../api/colex/chat.php';

$userId = '278d5983-d646-494f-a130-b22f7c321867';
$accounts = AnalyticsEngine::getUserAccounts($userId);

$oldTotal = 0.0;
foreach ($accounts as $acc) { $oldTotal += (float)($acc['current_balance'] ?? 0); }

$metrics = ['total_trades' => 5, 'net_pnl' => 100, 'win_rate' => 0.5]; // real shape, arbitrary values — only accounts/balance logic under test
$context = ['metrics' => $metrics, 'accounts' => $accounts];
$result = generateGeneralResponse($context, 'hi');

$cardBalance = null;
foreach (($result['cards'][0]['stats'] ?? []) as $stat) {
    if (($stat['label'] ?? '') === 'Total Balance') { $cardBalance = $stat['value']; }
}

echo "Accounts: " . count($accounts) . "\n";
echo "OLD (raw sum, computed independently): \$" . number_format($oldTotal, 2) . "\n";
echo "generateGeneralResponse()'s actual computed card value: {$cardBalance}\n";

echo ($cardBalance !== ('$' . number_format($oldTotal, 2)))
    ? "PASS — the function's own computed balance now differs from the old raw-sum formula, confirming the phase-aware repoint is active and doing real work (not silently falling through to the old behavior).\n"
    : "!!! Value matches the OLD raw sum exactly — either no prop-firm accounts differ for this user, or the fix isn't engaging.\n";
