<?php
ob_start();

require_once '../../engine/engine.php';
require_once 'indicators.php';

// POST-Check und Werte speichern
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	foreach ($indicators as $key => $meta) {
		if (isset($_POST[$key])) {
			$value = (float)$_POST[$key];
			$trend = $_POST[$key . '_trend'] ?? 'steigend';

			$stmt = $pdo->prepare("SELECT COUNT(*) FROM macro_indicators WHERE name = ?");
			$stmt->execute([$key]);
			$exists = $stmt->fetchColumn();

			if ($exists) {
				$stmt = $pdo->prepare("UPDATE macro_indicators SET value = ?, trend = ?, created_at = CURRENT_TIMESTAMP WHERE name = ?");
				$stmt->execute([$value, $trend, $key]);
			} else {
				$stmt = $pdo->prepare("INSERT INTO macro_indicators (name, value, threshold, trend) VALUES (?, ?, ?, ?)");
				$stmt->execute([$key, $value, $meta['threshold'], $trend]);
			}
		}
	}

	header("Location: ../../index.php");
	exit;
}