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

if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
  echo json_encode(['success' => false, 'error' => 'Invalid method']); exit;
}

$id   = (int)($_POST['portfolio_id'] ?? 0);
$name = trim((string)($_POST['name'] ?? ''));

if ($id <= 0 || $name === '') {
  echo json_encode(['success' => false, 'error' => 'Fehlende Angaben']); exit;
}

try {
  $stmt = $pdo->prepare("UPDATE depot_portfolios SET name = :name WHERE id = :id");
  $stmt->execute([':name' => $name, ':id' => $id]);
  echo json_encode(['success' => true]);
} catch (Throwable $e) {
  http_response_code(500);
  echo json_encode(['success' => false, 'error' => 'DB-Fehler: '.$e->getMessage()]);
}
