#!/usr/bin/env node
// Test script for Table transport testing
// Tests receiving 1 large and 1 small Tables via direct and link transport
// Uses NATSBridge.js smartreceive with "table" type
//
// Note: This test requires the apache-arrow library to deserialize table data.
// The JavaScript implementation uses apache-arrow for Arrow IPC deserialization.

const { smartreceive, log_trace } = require('./src/NATSBridge');

// Configuration
const SUBJECT = "/NATSBridge_table_test";
const NATS_URL = "nats.yiem.cc";

// Helper: Log with correlation ID
function log_trace(message) {
    const timestamp = new Date().toISOString();
    console.log(`[${timestamp}] ${message}`);
}

// Receiver: Listen for messages and verify Table handling
async function test_table_receive() {
    // Connect to NATS
    const { connect } = require('nats');
    const nc = await connect({ servers: [NATS_URL] });
    
    // Subscribe to the subject
    const sub = nc.subscribe(SUBJECT);
    
    for await (const msg of sub) {
        log_trace(`Received message on ${msg.subject}`);
        
        // Use NATSBridge.smartreceive to handle the data
        const result = await smartreceive(
            msg,
            {
                maxRetries: 5,
                baseDelay: 100,
                maxDelay: 5000
            }
        );
        
        // Result is an envelope dictionary with payloads field
        // Access payloads with result.payloads
        for (const { dataname, data, type } of result.payloads) {
            if (Array.isArray(data)) {
                log_trace(`Received Table '${dataname}' of type ${type}`);
                
                // Display table contents
                console.log(`  Dimensions: ${data.length} rows x ${data.length > 0 ? Object.keys(data[0]).length : 0} columns`);
                console.log(`  Columns: ${data.length > 0 ? Object.keys(data[0]).join(', ') : ''}`);
                
                // Display first few rows
                console.log(`  First 5 rows:`);
                for (let i = 0; i < Math.min(5, data.length); i++) {
                    console.log(`    Row ${i}: ${JSON.stringify(data[i])}`);
                }
                
                // Save to JSON file
                const fs = require('fs');
                const output_path = `./received_${dataname}.json`;
                const json_str = JSON.stringify(data, null, 2);
                fs.writeFileSync(output_path, json_str);
                log_trace(`Saved Table to ${output_path}`);
            } else {
                log_trace(`Received unexpected data type for '${dataname}': ${typeof data}`);
            }
        }
    }
    
    // Keep listening for 10 seconds
    setTimeout(() => {
        nc.close();
        process.exit(0);
    }, 120000);
}

// Run the test
console.log("Starting Table transport test...");
console.log("Note: This receiver will wait for messages from the sender.");
console.log("Run test_js_to_js_table_sender.js first to send test data.");

// Run receiver
console.log("testing smartreceive");
test_table_receive();

console.log("Test completed.");