<?php

$coin = getdbosql('db_coins', "symbol='VEX'");
if (!$coin) {
	echo '<section class="vexon-telemetry-card"><div class="vexon-empty-state">VEX coin configuration is not ready yet.</div></section>';
	return;
}

$t24 = time() - 24*60*60;
$blocks_24h = intval(dboscalar("SELECT COUNT(*) FROM blocks WHERE coin_id={$coin->id} AND time>$t24 AND category!='orphan'"));
$immature = intval(dboscalar("SELECT COUNT(*) FROM blocks WHERE coin_id={$coin->id} AND category='immature'"));
$confirmed = intval(dboscalar("SELECT COUNT(*) FROM blocks WHERE coin_id={$coin->id} AND category IN ('generate','generated')"));
$orphan = intval(dboscalar("SELECT COUNT(*) FROM blocks WHERE coin_id={$coin->id} AND category='orphan'"));
$last_block = getdbosql('db_blocks', "coin_id={$coin->id} ORDER BY id DESC");

$last_height = $last_block ? intval($last_block->height) : 0;
$last_status = $last_block ? htmlspecialchars($last_block->category, ENT_QUOTES, 'UTF-8') : 'Waiting';
$last_time = $last_block && $last_block->time ? datetoa2($last_block->time).' ago' : 'No blocks yet';
$reward = $coin->reward ? floatval($coin->reward) : floatval(VEXON_BLOCK_REWARD_ESTIMATE);
$immature_width = min(100, max(8, $immature*10));
$confirmed_width = min(100, max(8, $confirmed*10));
$orphan_width = min(100, max(8, $orphan*10));

echo <<<END
<section class="vexon-telemetry-card">
	<div class="vexon-card-heading">
		<div>
			<span class="vexon-card-kicker">Block flow</span>
			<h2>Vexon Rewards</h2>
		</div>
		<img src="/vexon/ui/images/vexon-logo.png" alt="Vexon">
	</div>

	<div class="vexon-metric-grid vexon-metric-grid-five">
		<div><span>Last block</span><strong>{$last_height}</strong></div>
		<div><span>Status</span><strong>{$last_status}</strong></div>
		<div><span>Found</span><strong>{$last_time}</strong></div>
		<div><span>24h blocks</span><strong>{$blocks_24h}</strong></div>
		<div><span>Reward target</span><strong>{$reward} VEX</strong></div>
	</div>

	<div class="vexon-chart-bars" aria-label="Vexon block status chart">
		<div><span>Immature</span><strong>{$immature}</strong><em style="width: {$immature_width}%"></em></div>
		<div><span>Confirmed</span><strong>{$confirmed}</strong><em style="width: {$confirmed_width}%"></em></div>
		<div><span>Rejected</span><strong>{$orphan}</strong><em style="width: {$orphan_width}%"></em></div>
	</div>
END;

$rows = getdbolist('db_blocks', "coin_id={$coin->id} ORDER BY id DESC LIMIT 8");
if (empty($rows)) {
	echo '<div class="vexon-empty-state">Waiting for the first Vexon block. Once miners find a block, reward status will appear here.</div>';
	echo '</section>';
	return;
}

echo '<table class="vexon-stat-table">';
echo '<thead><tr><th>Height</th><th>Reward</th><th>Difficulty</th><th>Found</th><th>Status</th></tr></thead><tbody>';
foreach ($rows as $block) {
	$height = intval($block->height);
	$amount = bitcoinvaluetoa($block->amount).' VEX';
	$diff = Itoa2($block->difficulty, 3);
	$found = $block->time ? datetoa2($block->time).' ago' : '-';
	$status = htmlspecialchars($block->category, ENT_QUOTES, 'UTF-8');
	echo "<tr><td>{$height}</td><td>{$amount}</td><td>{$diff}</td><td>{$found}</td><td><span class=\"vexon-status-pill\">{$status}</span></td></tr>";
}
echo '</tbody></table>';
echo '</section>';
