{%- import "util.include.jinja" as util -%}
{%- import "cloudevents.jinja.include" as cloudEvents -%}
{%- set messagegroups = root.messagegroups %}
"""
Sample usage of {{ main_project_name }}

This example demonstrates how to send messages using the AMQP 1.0 producer.
"""
import sys
from {{ main_project_name }} import *

def main():
    """Main function"""
    {% for messagegroupid, messagegroup in messagegroups.items() %}
    {%- set class_name = (messagegroupid | pascal | strip_dots) + "Producer" %}
    
    # Create producer
    print("Creating AMQP producer...")
    producer = {{ class_name }}(
        host="localhost",  # Change to your AMQP broker hostname
        address="my-queue",  # Change to your queue/topic name
        port=5672,  # Use 5671 for TLS
        username="guest",  # Change to your username
        password="guest",  # Change to your password
        content_mode='structured',  # or 'binary' for CloudEvents
        format_type='application/json'
    )
    
    try:
        {% for messageid, message in messagegroup.messages.items() %}
        {%- set messagename = messageid | pascal | strip_namespace %}
        {%- set isCloudEvent = (message | exists("envelope","CloudEvents/1.0")) %}
        {%- set type_name = util.DeclareDataType( data_project_name, root, message ) | strip_namespace %}
        
        # Send {{ messagename }} message
        print("Sending {{ messagename }} message...")
        # TODO: Create a {{ type_name }} instance with actual data
        # data = {{ type_name }}(...)
        # producer.send_{{ messagename | snake }}(
        #     data=data,
        {%- if isCloudEvent %}
        {%- for attrname, attribute in message.envelopemetadata.items() if attribute.value is not defined and attrname not in ["time", "id", "datacontenttype", "dataschema"] and attribute.required %}
        #     _{{ attrname }}="value",  # Set appropriate value
        {%- endfor %}
        {%- endif %}
        #     content_type="application/json"
        # )
        # print("{{ messagename }} message sent successfully!")
        
        {% endfor %}
        print("\nAll messages sent successfully!")
        
    except Exception as e:
        print(f"Error sending messages: {e}", file=sys.stderr)
        return 1
    finally:
        producer.close()
        print("Producer closed")
    
    return 0
    {% endfor %}

if __name__ == "__main__":
    sys.exit(main())
