<?php

$USE_CACHE = false;

require_once __DIR__ . '/../../engine/api-handling/api_helpers.php';

$symbol = $_GET['symbol'] ?? '';
$group  = $_GET['group'] ?? '';
$asof   = $_GET['asof'] ?? null;
$target_pe = $_GET['target_pe'] ?? null;

if (!$symbol || !$group) {
	http_response_code(400);
	echo "<div class='text-danger'>Symbol oder Gruppe fehlt.</div>";
	exit;
}

$cacheDir = __DIR__ . '/cache';
if (!is_dir($cacheDir)) mkdir($cacheDir, 0777, true);

$cacheKey  = "{$symbol}_{$group}_" . ($asof ?: 'latest') . '.html';
$cacheFile = $cacheDir . '/' . $cacheKey;

if ($USE_CACHE && file_exists($cacheFile) && filemtime($cacheFile) > time() - 3600) {
	echo file_get_contents($cacheFile);
	exit;
}

$GLOBALS['tab_symbol'] = $symbol;
$GLOBALS['tab_asof']   = $asof;
$GLOBALS['tab_target_pe'] = $target_pe;

ob_start();

switch ($group) {
	case 'fundamental_basic':
		include __DIR__ . '/tab_fundamental.php';
		break;
	case 'profile':
		include __DIR__ . '/tab_profile.php';
		break;
	case 'strategic-profile':
		include __DIR__ . '/tab_strategic-profile.php';
		break;
	case 'technical':
		include __DIR__ . '/tab_technical.php';
		break;
	case 'berichte':
		include __DIR__ . '/tab_berichte.php';
		break;
	case 'analyst_rating':
		include __DIR__ . '/tab_analyst.php';
		break;
	case 'dividends':
		include __DIR__ . '/tab_dividends.php';
		break;
	case 'insider':
		include __DIR__ . '/tab_insider.php';
		break;
	case 'news':
		include __DIR__ . '/tab_news.php';
		break;
	case 'seasonality':
		include __DIR__ . '/tab_saisonalitaet.php';
		break;
	case 'minervini':
		include __DIR__ . '/tab_minervini.php';
		break;
	case 'korrelationen':
			include __DIR__ . '/tab_korrelationen.php';
			break;
	case 'forecast':
			include __DIR__ . '/tab_forecast.php';
			break;
	default:
		echo "<p class='text-warning'>Unbekannte Gruppe: $group</p>";
}

$content = ob_get_clean();

if ($USE_CACHE) {
	file_put_contents($cacheFile, $content);
}

echo $content;
?>