<?php
require_once __DIR__ . '/../includes/functions.php';
startSecureSession();
requireAdmin();

$orders = readJson(ORDERS_FILE);
$users = readJson(USERS_FILE);
$usersById = [];
foreach ($users as $u) {
    $usersById[$u['id']] = $u;
}

$sorted = array_reverse($orders);
$recent = array_slice($sorted, 0, 25);

$tx = [];
foreach ($recent as $o) {
    $u = !empty($o['user_id']) ? ($usersById[$o['user_id']] ?? null) : null;
    $tx[] = [
        'order_id' => $o['order_id'] ?? '',
        'user' => $u ? ($u['email'] ?? 'user') : '',
        'type' => $o['type'] ?? '',
        'amount' => number_format(floatval($o['amount'] ?? 0), 2),
        'status' => $o['status'] ?? '',
        'utr' => $o['utr'] ?? '',
        'created_at' => $o['created_at'] ?? ''
    ];
}

jsonResponse(['status' => 'success', 'total' => count($orders), 'transactions' => $tx]);