<?php
declare(strict_types=1);
require_once dirname(__DIR__) . '/_bootstrap.php';

golder_require_method('POST');
$ctx = golder_api_auth('desktop_sync');
$customer = sync_api_require_profile_sync_customer((int)($ctx['customer_id'] ?? 0));
$input = golder_api_input();
$profileId = trim((string)($input['profile_id'] ?? ''));

if ($profileId === '') sync_api_json_error(400, 'INVALID', 'profile_id is required.');

try {
    $tenant = golder_tenant_pdo_from_customer($customer);
    sync_api_ensure_deleted_profiles_table($tenant);

    // İşlem (Transaction) kullanmadan dümdüz silme yapıyoruz. Hata riskini sıfıra indirir.
    sync_api_record_profile_tombstone($tenant, $profileId, "Deleted from API", sync_api_uuid4(), "WEB");
    sync_api_delete_profile_rows($tenant, $profileId);

    golder_json_response(['status' => 'ok', 'deleted' => true]);
} catch (Throwable $e) {
    sync_api_json_error(500, 'DELETE_ERROR', $e->getMessage());
}

