<?php
/**
 * Live, end-to-end verification of api/account/state.php's
 * PropFirmRuleEngine wiring, through the real Apache/PHP/MariaDB stack.
 * Read-only — both accounts used are real, already-existing accounts;
 * no mutation, no revert needed.
 *
 * Case A: d207ae67-... — a real Phase-2 account whose rules JSON declares
 * a different Phase 1 (8%) vs Phase 2 (5%) profit target. Proves the
 * cascade-#5 fix: OLD code's `$rules['profit_target_pct'] ?? $rules['phase_1_target_pct']`
 * would show 8% (Phase 1's target, wrong) for this Phase-2 account; NEW
 * code shows 5% (the real, phase-cascaded target).
 *
 * Case B: e2f9fa70-... — a real Phase-1 account currently in profit
 * (phase_balance > starting_balance). Proves the drawdown-clamp fix: OLD
 * code's unclamped `(startingBalance - currentBalance) / startingBalance * 100`
 * would show a NEGATIVE drawdown_pct for an in-profit account; NEW code
 * shows the canonical, clamped-at-zero 0%.
 */
require_once __DIR__ . '/../config/config.php';

$base = 'http://localhost:8080/traderalign';
$cookieJar = tempnam(sys_get_temp_dir(), 'zzcookie2');

function loginAs(string $base, string $uid, string $cookieJar): void {
    $ch = curl_init("{$base}/public/_zz_test_autologin.php?uid=" . urlencode($uid));
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_COOKIEJAR => $cookieJar,
        CURLOPT_COOKIEFILE => $cookieJar,
    ]);
    curl_exec($ch);
    curl_close($ch);
}

function fetchState(string $base, string $accountId, string $cookieJar): array {
    $ch = curl_init("{$base}/api/account/state?account_id=" . urlencode($accountId));
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_COOKIEJAR => $cookieJar,
        CURLOPT_COOKIEFILE => $cookieJar,
    ]);
    $resp = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return [$code, $resp];
}

echo "=== CASE A — Phase-2 account, differing declared targets (cascade-#5 fix) ===\n";
$accountA = 'd207ae67-78fb-4951-8e1c-403b699d618a';
$userA = '278d5983-d646-494f-a130-b22f7c321867';
loginAs($base, $userA, $cookieJar);
[$codeA, $respA] = fetchState($base, $accountA, $cookieJar);
echo "HTTP {$codeA}\n{$respA}\n";
$dA = json_decode($respA, true);
echo "OLD code would have shown profit_target_pct = 8 (Phase 1's key, wrong for a Phase-2 account)\n";
echo "NEW code shows profit_target_pct = " . json_encode($dA['profit_target_pct'] ?? null) . " (real Phase 2 target)\n\n";

echo "=== CASE B — In-profit account, drawdown clamping fix ===\n";
$accountB = 'e2f9fa70-6034-46b9-a0ae-a7b9239bdff9';
$userB = '00b8dcb1-74b6-4f8f-b10b-a70b584b73cf';
loginAs($base, $userB, $cookieJar);
[$codeB, $respB] = fetchState($base, $accountB, $cookieJar);
echo "HTTP {$codeB}\n{$respB}\n";
$dB = json_decode($respB, true);
$oldSigned = round((($dB['starting_balance'] ?? 0) - ($dB['current_balance'] ?? 0)) / max(0.01, $dB['starting_balance'] ?? 1) * 100, 2);
echo "OLD code's unclamped formula on these same real numbers would show drawdown_pct = {$oldSigned}% (negative — wrong, implies drawdown while in profit)\n";
echo "NEW code shows drawdown_pct = " . json_encode($dB['drawdown_pct'] ?? null) . " (canonical, clamped at zero)\n";

@unlink($cookieJar);
