<?php
header('Content-Type: application/json');

if (!isset($_GET['transactionId'])) {
    echo json_encode(['transactionId' => null, 'invoices' => [], 'warranty' => []]);
    exit;
}

$transactionId = intval($_GET['transactionId']);

$invoiceDir = __DIR__ . "/uploads/transactions/transaction_$transactionId/invoices/";
$warrantyDir = __DIR__ . "/uploads/transactions/transaction_$transactionId/warranty/";

function getFiles($dir) {
    $files = [];
    if (is_dir($dir)) {
        foreach (scandir($dir) as $file) {
            if ($file === '.' || $file === '..') continue;
            $files[] = $file; // only the file name
        }
    }
    return $files;
}

echo json_encode([
    'transactionId' => $transactionId, // added for debug
    'invoices' => getFiles($invoiceDir, "uploads/transactions/transaction_$transactionId/invoices/"),
    'warranty' => getFiles($warrantyDir, "uploads/transactions/transaction_$transactionId/warranty/")
]);
?>
