<?php
/**
 * Live, end-to-end verification of AccountPicker::apChallengeRulesSummary()'s
 * Wave-3d fix, via a real Trading Center Account Home render.
 * Construct-verify-revert: real accounts in this DB never carry
 * phase-specific max_loss/daily_loss keys today (confirmed earlier this
 * session), so this temporarily adds them, differing from the account's
 * real flat keys, to prove the card now shows the phase-specific value.
 */
require_once __DIR__ . '/../config/config.php';

$pdo = new PDO(
    "mysql:host=" . DB_HOST . ";port=" . DB_PORT . ";dbname=" . DB_NAME . ";charset=utf8mb4",
    DB_USER, DB_PASS,
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]
);

$accountId = 'd207ae67-78fb-4951-8e1c-403b699d618a'; // Phase Test 5K - Attempt 1, phase_2_active
$userId = '278d5983-d646-494f-a130-b22f7c321867';

$stmt = $pdo->prepare("SELECT rules FROM challenge_accounts WHERE account_id = ?");
$stmt->execute([$accountId]);
$originalRules = $stmt->fetchColumn();
echo "Original rules: {$originalRules}\n";

$decoded = json_decode($originalRules, true);
$decoded['phase_2_max_loss'] = 12;
$decoded['phase_2_daily_loss'] = 6;
$mutatedJson = json_encode($decoded);
$pdo->prepare("UPDATE challenge_accounts SET rules = ? WHERE account_id = ?")->execute([$mutatedJson, $accountId]);
echo "Mutated rules to: {$mutatedJson}\n\n";

$base = 'http://localhost:8080/traderalign';
$cookieJar = tempnam(sys_get_temp_dir(), 'zzcookie16');
$ch = curl_init("{$base}/public/_zz_test_autologin.php?uid=" . urlencode($userId));
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_COOKIEJAR => $cookieJar, CURLOPT_COOKIEFILE => $cookieJar]);
curl_exec($ch);
curl_close($ch);

$url = "{$base}/trading-center?hub=accounts&kind=prop&view=center&challenge=two_step&stage=phase_2&account_id=" . urlencode($accountId);
$ch = curl_init($url);
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);
@unlink($cookieJar);

echo "HTTP {$code}\n";
$hasError = (stripos($resp, 'Warning:') !== false || stripos($resp, 'Fatal error') !== false || stripos($resp, 'Notice:') !== false);
echo 'PHP error: ' . ($hasError ? 'YES !!!' : 'no') . "\n";

preg_match('/Maximum Drawdown<\/span><span>([^<]+)/', $resp, $mMax);
preg_match('/Daily Drawdown<\/span><span>([^<]+)/', $resp, $mDaily);
echo "Rendered Maximum Drawdown: " . ($mMax[1] ?? 'NOT FOUND') . " (expect 12%, not the flat 8%)\n";
echo "Rendered Daily Drawdown: " . ($mDaily[1] ?? 'NOT FOUND') . " (expect 6%, not the flat 4%)\n";

$pass = (($mMax[1] ?? '') === '12%') && (($mDaily[1] ?? '') === '6%');
echo $pass ? "PASS\n\n" : "!!! FAIL\n\n";

// Revert
$pdo->prepare("UPDATE challenge_accounts SET rules = ? WHERE account_id = ?")->execute([$originalRules, $accountId]);
$stmt = $pdo->prepare("SELECT rules FROM challenge_accounts WHERE account_id = ?");
$stmt->execute([$accountId]);
$afterRevert = $stmt->fetchColumn();
echo "Reverted rules: {$afterRevert}\n";
echo "Revert confirmed byte-identical: " . ($afterRevert === $originalRules ? 'YES' : 'NO — MISMATCH') . "\n";
