<?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');
	
	$id = (int)($_POST['id'] ?? 0);
	$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 ($id <= 0 || $pid <= 0 || $label === '' || $value < 0) throw new RuntimeException('Ungültige Eingaben');
	
	$stmt = $pdo->prepare("UPDATE depot_manual_assets
	SET label = :label, category = :category, value = :value, currency = :currency, notes = :notes
	WHERE id = :id AND portfolio_id = :pid");
	$stmt->execute([
		':id' => $id,
		':pid' => $pid,
		':label' => $label,
		':category' => $category,
		':value' => $value,
		':currency' => $currency,
		':notes' => $notes,
	]);
	
	echo json_encode(['success' => true]);
} catch (Throwable $e) {
	http_response_code(400);
	echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}