<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/_bootstrap.php';

golder_require_method('POST');
$ctx = golder_api_auth('desktop_sync');
$input = golder_api_input();

$profileIds = $input['profile_ids'] ?? [];
if (!is_array($profileIds)) {
    $singleProfileId = trim((string)($input['profile_id'] ?? ''));
    $profileIds = ($singleProfileId !== '') ? [$singleProfileId] : [];
}
if (!$profileIds) {
    golder_json_response(['status' => 'error', 'message' => 'profile_id or profile_ids is required'], 400);
}

$result = golder_create_backup_bundle($ctx['master'], $ctx['customer'], $profileIds, 'manual');
if (empty($result['ok'])) {
    golder_json_response(['status' => 'error', 'message' => (string)($result['message'] ?? 'Backup failed')], 400);
}

golder_json_response(['status' => 'ok', 'backup' => $result]);


