#!/usr/bin/env python3
"""
Test script for mixed payload testing - Micropython
Tests sending mixed payload types 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_mixed_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 payloads for mixed content test
    
    # 1. Small text (direct transport)
    text_data = "Hello, this is a text message for testing mixed payloads!"
    
    # 2. Small dictionary (direct transport)
    dict_data = {
        "status": "ok",
        "code": 200,
        "message": "Test successful",
        "items": [1, 2, 3]
    }
    
    # 3. Small binary (direct transport)
    binary_data = b"\x00\x01\x02\x03\x04\x05" + b"\xff" * 100
    
    # 4. Large text (link transport - will use fileserver)
    large_text = "\n".join([
        f"Line {i}: This is a large text payload for link transport testing. " * 50
        for i in range(100)
    ])
    
    # Test data list - mixed payload types
    data = [
        ("message_text", text_data, "text"),
        ("config_dict", dict_data, "dictionary"),
        ("small_binary", binary_data, "binary"),
        ("large_text", large_text, "text"),
    ]
    
    log_trace(correlation_id, f"Starting smartsend for subject: {SUBJECT}")
    log_trace(correlation_id, f"Correlation ID: {correlation_id}")
    
    # Use smartsend with mixed types
    env, env_json_str = smartsend(
        SUBJECT,
        data,  # 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="mixed_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()