<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/_admin_auth.php';
require_once dirname(__DIR__, 2) . '/common/payment_service.php';

golder_require_method('POST');
$adminUser = golder_api_admin_auth();
$master = golder_master_pdo();
golder_ensure_payment_tables($master);

$input = golder_api_input();
$requestId = trim((string)($input['payment_request_id'] ?? ''));
$message = trim((string)($input['admin_message'] ?? 'Approved'));
if ($requestId === '') {
    golder_json_response(['status' => 'error', 'message' => 'payment_request_id is required'], 400);
}

$result = golder_approve_payment_request($master, $requestId, $message, $adminUser);
if (empty($result['ok'])) {
    golder_json_response(['status' => 'error', 'message' => (string)($result['message'] ?? 'Approval failed')], 409);
}

golder_json_response([
    'status' => 'ok',
    'customer_id' => (int)($result['customer_id'] ?? 0),
    'credentials' => $result['credentials'] ?? null,
    'application_username' => (string)($result['application_username'] ?? ''),
    'mail' => $result['mail'] ?? null,
]);


