<?php 
require_once __DIR__ . '/engine/engine.php';
require_once __DIR__ . '/engine/api-handling/fmp_client.php';
require_once __DIR__ . '/views/header.php';
require_once __DIR__ . '/views/navigation.php';

function fetch_market_movers($type = 'gainers') {
	$endpointMap = [
		'gainers'     => ['key' => 'stock_market_gainers', 'vars' => []],
		'losers'      => ['key' => 'stock_market_losers',  'vars' => []],
		'most_active' => ['key' => 'stock_market_actives', 'vars' => []],
	];

	if (!isset($endpointMap[$type])) {
		return [];
	}

	$resp = fmp_multi([
		'movers' => $endpointMap[$type]
	], 3);

	$data = $resp['movers'] ?? [];

	if (is_string($data)) {
		$tmp = json_decode($data, true);
		if (json_last_error() === JSON_ERROR_NONE) {
			$data = $tmp;
		}
	}
	if (is_object($data)) {
		$data = json_decode(json_encode($data), true);
	}
	if (isset($data['body'])) $data = $data['body'];
	if (isset($data['data'])) $data = $data['data'];

	return is_array($data) ? $data : [];
}

$gainers = fetch_market_movers('gainers');
$losers  = fetch_market_movers('losers');
$actives = fetch_market_movers('most_active');;
?>

<main class="flex-grow-1">
	<section>
		<div class="container my-5">
			<h1 class="mb-4">Market Movers</h1>
			<div class="row g-4">
				<div class="col-lg-4">
					<div class="card h-100">
						<div class="card-header">Top Gainers</div>
						<div class="card-body">
							<table class="table table-sm mb-0">
								<thead><tr><th>Symbol</th><th>Preis</th><th>%</th></tr></thead>
								<tbody>
									<?php foreach (array_slice($gainers, 0, 10) as $g): ?>
										<tr>
											<td><strong><?= $g['symbol'] ?></strong></td>
											<td><?= number_format($g['price'], 2) ?> $</td>
											<td class="text-success"><?= number_format($g['changesPercentage'], 2) ?>%</td>
										</tr>
									<?php endforeach; ?>
								</tbody>
							</table>
						</div>
					</div>
				</div>
				<div class="col-lg-4">
					<div class="card h-100">
						<div class="card-header">Top Losers</div>
						<div class="card-body">
							<table class="table table-sm mb-0">
								<thead><tr><th>Symbol</th><th>Preis</th><th>%</th></tr></thead>
								<tbody>
									<?php foreach (array_slice($losers, 0, 10) as $l): ?>
										<tr>
											<td><strong><?= $l['symbol'] ?></strong></td>
											<td><?= number_format($l['price'], 2) ?> $</td>
											<td class="text-danger"><?= number_format($g['changesPercentage'], 2) ?>%</td>
										</tr>
									<?php endforeach; ?>
								</tbody>
							</table>
						</div>
					</div>
				</div>
				<div class="col-lg-4">
					<div class="card h-100">
						<div class="card-header">Meistgehandelt</div>
						<div class="card-body">
							<table class="table table-sm mb-0">
								<thead><tr><th>Symbol</th><th>Preis</th><th>Volumen</th></tr></thead>
								<tbody>
									<?php foreach (array_slice($actives, 0, 10) as $a): ?>
										<tr>
											<td><strong><?= $a['symbol'] ?></strong></td>
											<td><?= number_format($a['price'], 2) ?> $</td>
											<td>
											  <?= isset($a['volume']) && is_numeric($a['volume']) ? number_format($a['volume']) : '–' ?>
											</td>
										</tr>
									<?php endforeach; ?>
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
    </section>
</main>

<?php require_once __DIR__ . '/views/footer.php'; ?>