<?php
require_once __DIR__ . '/../../engine/engine.php';
header('Content-Type: application/json; charset=utf-8');

try {
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') throw new RuntimeException('POST required');

	$pid = (int)($_POST['portfolio_id'] ?? 0);
	$label = trim($_POST['label'] ?? '');
	$category = trim($_POST['category'] ?? 'Other');
	$value = (float)($_POST['value'] ?? 0);
	$currency = trim($_POST['currency'] ?? 'EUR');
	$notes = trim($_POST['notes'] ?? '');

	if ($pid <= 0 || $label === '' || $value < 0) throw new RuntimeException('Ungültige Eingaben');

	$stmt = $pdo->prepare("INSERT INTO depot_manual_assets (portfolio_id, label, category, value, currency, notes)
	VALUES (:pid, :label, :category, :value, :currency, :notes)");
	$stmt->execute([
		':pid' => $pid,
		':label' => $label,
		':category' => $category,
		':value' => $value,
		':currency' => $currency,
		':notes' => $notes,
	]);

	echo json_encode(['success' => true, 'id' => (int)$pdo->lastInsertId()]);
} catch (Throwable $e) {
	http_response_code(400);
	echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}