#!/usr/bin/env python3
"""
Test script for dictionary transport testing - Micropython
Tests sending dictionary messages via NATS using nats_bridge.py smartsend
"""

import sys
import os

# Add src to path for import
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))

from nats_bridge import smartsend, log_trace
import uuid

# Configuration
SUBJECT = "/NATSBridge_dict_test"
NATS_URL = "nats://nats.yiem.cc:4222"
FILESERVER_URL = "http://192.168.88.104:8080"
SIZE_THRESHOLD = 1_000_000  # 1MB

# Create correlation ID for tracing
correlation_id = str(uuid.uuid4())


def main():
    # Create a small dictionary (will use direct transport)
    small_dict = {
        "name": "test",
        "value": 42,
        "enabled": True,
        "metadata": {
            "version": "1.0.0",
            "timestamp": "2026-02-22T12:00:00Z"
        }
    }
    
    # Create a large dictionary (will use link transport if > 1MB)
    # Generate a larger dictionary (~2MB to ensure link transport)
    large_dict = {
        "id": str(uuid.uuid4()),
        "items": [
            {
                "index": i,
                "name": f"item_{i}",
                "value": i * 1.5,
                "data": "x" * 10000  # Large string per item
            }
            for i in range(200)
        ],
        "metadata": {
            "count": 200,
            "created": "2026-02-22T12:00:00Z"
        }
    }
    
    # Test data 1: small dictionary
    data1 = ("small_dict", small_dict, "dictionary")
    
    # Test data 2: large dictionary
    data2 = ("large_dict", large_dict, "dictionary")
    
    log_trace(correlation_id, f"Starting smartsend for subject: {SUBJECT}")
    log_trace(correlation_id, f"Correlation ID: {correlation_id}")
    
    # Use smartsend with dictionary type
    env, env_json_str = smartsend(
        SUBJECT,
        [data1, data2],  # List of (dataname, data, type) tuples
        nats_url=NATS_URL,
        fileserver_url=FILESERVER_URL,
        size_threshold=SIZE_THRESHOLD,
        correlation_id=correlation_id,
        msg_purpose="chat",
        sender_name="dict_sender",
        receiver_name="",
        receiver_id="",
        reply_to="",
        reply_to_msg_id="",
        is_publish=True  # Publish the message to NATS
    )
    
    log_trace(correlation_id, f"Sent message with {len(env.payloads)} payloads")
    
    # Log transport type for each payload
    for i, payload in enumerate(env.payloads):
        log_trace(correlation_id, f"Payload {i+1} ('{payload.dataname}'):")
        log_trace(correlation_id, f"  Transport: {payload.transport}")
        log_trace(correlation_id, f"  Type: {payload.type}")
        log_trace(correlation_id, f"  Size: {payload.size} bytes")
        log_trace(correlation_id, f"  Encoding: {payload.encoding}")
        
        if payload.transport == "link":
            log_trace(correlation_id, f"  URL: {payload.data}")
    
    print(f"Test completed. Correlation ID: {correlation_id}")


if __name__ == "__main__":
    main()