<?php
declare(strict_types=1);

require_once __DIR__ . '/_bootstrap.php';

golder_require_method('GET');
$master = golder_master_pdo();
$user = golder_portal_api_require_login($master);
$userId = (int)($user['id'] ?? 0);

golder_ensure_payment_tables($master);

$limit = max(1, min(200, (int)($_GET['limit'] ?? 100)));

$stPay = $master->prepare(
    'SELECT id, payment_code, full_name, email, phone,
            requested_plan_type, requested_pc_devices, requested_web_sessions,
            duration_key, amount, submitted_amount, calculated_amount, amount_delta, amount_mismatch_flag,
            currency, status, admin_message, created_at, updated_at, approved_at, rejected_at
     FROM payment_requests
     WHERE portal_user_id = :uid
     ORDER BY created_at DESC
     LIMIT ' . (int)$limit
);
$stPay->execute([':uid' => $userId]);
$payments = $stPay->fetchAll() ?: [];

$demos = golder_demo_request_list_for_portal($master, $userId, $limit);
foreach ($demos as &$row) {
    $row['demo_password'] = golder_decrypt_demo_secret((string)($row['demo_password_enc'] ?? ''));
    unset($row['demo_password_enc']);
}
unset($row);

$support = golder_support_threads_for_portal($master, $userId, $limit);

golder_json_response([
    'status' => 'ok',
    'payments' => $payments,
    'demos' => $demos,
    'support' => $support,
]);


