<?php
require_once __DIR__ . '/../../engine/engine.php';

$amount = $_POST['amount'] ?? null;
if (!is_numeric($amount)) {
	http_response_code(400);
	echo "Ungültiger Betrag.";
	exit;
}

// Multi-Depot: portfolio_id aus POST (fallback 1)
$portfolioId = isset($_POST['portfolio_id']) ? (int)$_POST['portfolio_id']
			  : (isset($_POST['pid']) ? (int)$_POST['pid'] : 1);

// Prüfen, ob depot_cash.portfolio_id existiert (sanfter Übergang)
$hasPid = false;
try {
	$chk = $pdo->prepare("SHOW COLUMNS FROM `depot_cash` LIKE 'portfolio_id'");
	$chk->execute();
	$hasPid = (bool)$chk->fetchColumn();
} catch (Throwable $e) {
	$hasPid = false;
}

if ($hasPid) {
	$stmt = $pdo->prepare("INSERT INTO depot_cash (amount, portfolio_id) VALUES (:amount, :pid)");
	$stmt->execute([
		':amount' => (float)$amount,
		':pid'    => $portfolioId
	]);
} else {
	// Legacy-Fallback ohne portfolio_id
	$stmt = $pdo->prepare("INSERT INTO depot_cash (amount) VALUES (:amount)");
	$stmt->execute([':amount' => (float)$amount]);
}

echo 'success';