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

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

// Configuration
const SUBJECT = "/NATSBridge_table_test";
const NATS_URL = "nats.yiem.cc";
const FILESERVER_URL = "http://192.168.88.104:8080";

// Create correlation ID for tracing
const correlation_id = uuid4();

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

// File upload handler for plik server
async function plik_upload_handler(fileserver_url, dataname, data, correlation_id) {
    log_trace(correlation_id, `Uploading ${dataname} to fileserver: ${fileserver_url}`);
    
    // Step 1: Get upload ID and token
    const url_getUploadID = `${fileserver_url}/upload`;
    const headers = {
        "Content-Type": "application/json"
    };
    const body = JSON.stringify({ OneShot: true });
    
    let response = await fetch(url_getUploadID, {
        method: "POST",
        headers: headers,
        body: body
    });
    
    if (!response.ok) {
        throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
    }
    
    const responseJson = await response.json();
    const uploadid = responseJson.id;
    const uploadtoken = responseJson.uploadToken;
    
    // Step 2: Upload file data
    const url_upload = `${fileserver_url}/file/${uploadid}`;
    
    // Create multipart form data
    const formData = new FormData();
    const blob = new Blob([data], { type: "application/octet-stream" });
    formData.append("file", blob, dataname);
    
    response = await fetch(url_upload, {
        method: "POST",
        headers: {
            "X-UploadToken": uploadtoken
        },
        body: formData
    });
    
    if (!response.ok) {
        throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
    }
    
    const fileResponseJson = await response.json();
    const fileid = fileResponseJson.id;
    
    // Build the download URL
    const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
    
    log_trace(correlation_id, `Uploaded to URL: ${url}`);
    
    return {
        status: response.status,
        uploadid: uploadid,
        fileid: fileid,
        url: url
    };
}

// Sender: Send Tables via smartsend
async function test_table_send() {
    // Note: This test requires apache-arrow library to create Arrow IPC data.
    // For now, we'll use a simple array of objects as table data.
    // In production, you would use the apache-arrow library to create Arrow IPC data.
    
    // Create a small Table (will use direct transport)
    const small_table = [
        { id: 1, name: "Alice", score: 95 },
        { id: 2, name: "Bob", score: 88 },
        { id: 3, name: "Charlie", score: 92 }
    ];
    
    // Create a large Table (will use link transport if > 1MB)
    // Generate a larger dataset (~2MB to ensure link transport)
    const large_table = [];
    for (let i = 0; i < 50000; i++) {
        large_table.push({
            id: i,
            message: `msg_${i}`,
            sender: `sender_${i}`,
            timestamp: new Date().toISOString(),
            priority: Math.floor(Math.random() * 3) + 1
        });
    }
    
    // Test data 1: small Table
    const data1 = { dataname: "small_table", data: small_table, type: "table" };
    
    // Test data 2: large Table
    const data2 = { dataname: "large_table", data: large_table, type: "table" };
    
    // Use smartsend with table type
    // For small Table: will use direct transport (Arrow IPC encoded)
    // For large Table: will use link transport (uploaded to fileserver)
    const { env, env_json_str } = await smartsend(
        SUBJECT,
        [data1, data2],
        {
            natsUrl: NATS_URL,
            fileserverUrl: FILESERVER_URL,
            fileserverUploadHandler: plik_upload_handler,
            sizeThreshold: 1_000_000,
            correlationId: correlation_id,
            msgPurpose: "chat",
            senderName: "table_sender",
            receiverName: "",
            receiverId: "",
            replyTo: "",
            replyToMsgId: "",
            isPublish: true  // Publish the message to NATS
        }
    );
    
    log_trace(`Sent message with ${env.payloads.length} payloads`);
    
    // Log transport type for each payload
    for (let i = 0; i < env.payloads.length; i++) {
        const payload = env.payloads[i];
        log_trace(`Payload ${i + 1} ('${payload.dataname}'):`);
        log_trace(`  Transport: ${payload.transport}`);
        log_trace(`  Type: ${payload.type}`);
        log_trace(`  Size: ${payload.size} bytes`);
        log_trace(`  Encoding: ${payload.encoding}`);
        
        if (payload.transport === "link") {
            log_trace(`  URL: ${payload.data}`);
        }
    }
}

// Run the test
console.log("Starting Table transport test...");
console.log(`Correlation ID: ${correlation_id}`);

// Run sender
console.log("start smartsend for tables");
test_table_send();

console.log("Test completed.");