<?php
$symbol     = $GLOBALS['tab_symbol'] ?? '';

require_once __DIR__ . '/../../engine/translation/translate_helper.php';
require_once __DIR__ . '/../../engine/api-handling/fmp_client.php';

$data = [];
$news_items = [];
// --- FMP data via fmp_client.php (reines Daten-Wiring, keine Logik-Änderung) ---
try {
	if (!isset($symbol) || !$symbol) {
		throw new \RuntimeException('Symbol fehlt.');
	}
	if (!function_exists('fmp_multi')) {
		throw new \RuntimeException('fmp_multi() nicht verfügbar – bitte fmp_client.php einbinden.');
	}

	$resp = fmp_multi([
		'news' => ['key' => 'stock_news', 'vars' => ['symbol' => $symbol], 'query' => ['limit' => 50]],
	], 10);

	$news_items = $resp['news'] ?? [];
	
	if (empty($news_items)) {
		?>
		<div class="row mt-3">
			<div class="col-12">
				<div class="alert alert-warning text-center">
					Für <strong><?= htmlspecialchars($symbol) ?></strong> liegen keine Nachrichten vor.
				</div>
			</div>
		</div>
		<?php
		return;
	}
	
} catch (\Throwable $ex) {
	error_log('[tab_news] FMP Fetch Error: ' . $ex->getMessage());
}
// --- End Daten-Wiring ---

?>

<?php if (!empty($news_items)): ?>
<div class="row">
	<div class="col-12">
		<div class="card">
			<div class="card-body">
				<h5>Aktuelle Unternehmensnews:</h5>
				<ul class="list-unstyled mb-0 mt-3">
				<?php foreach ($news_items as $item): ?>
					<li class="my-1">
						<div class="fw-semibold">
							<?php
								$title = $item['title'];
								$title_de = function_exists('translate_libre') ? translate_libre($title, 'en', 'de') : $title;
							?>
							<a href="<?= htmlspecialchars($item['url']) ?>" target="_blank" rel="noopener" class="text-decoration-none text-dark">
								<?= htmlspecialchars($title_de) ?>
							</a>
						</div>
						<div class="text-muted small">
							<?= date('d.m.Y H:i', strtotime($item['publishedDate'])) ?> · <?= htmlspecialchars($item['site']) ?>
						</div>
					</li>
				<?php endforeach; ?>
				</ul>
			</div>
		</div>
	</div>
</div>
<?php endif; ?>