import asyncio
import base64
import json
import os
import random

from dotenv import load_dotenv

from mtmaisdk import new_client
from mtmaisdk.clients.admin import TriggerWorkflowOptions
from mtmaisdk.clients.run_event_listener import StepRunEventType
from mtmaisdk.v2.hatchet import Hatchet


async def main():
    load_dotenv()
    hatchet = Hatchet()

    # Generate a random stream key to use to track all
    # stream events for this workflow run.

    streamKey = "streamKey"
    streamVal = f"sk-{random.randint(1, 100)}"

    # Specify the stream key as additional metadata
    # when running the workflow.

    # This key gets propagated to all child workflows
    # and can have an arbitrary property name.

    workflowRun = hatchet.admin.run_workflow(
        "Parent",
        {"n": 2},
        options={"additional_metadata": {streamKey: streamVal}},
    )

    # Stream all events for the additional meta key value
    listener = hatchet.listener.stream_by_additional_metadata(streamKey, streamVal)

    async for event in listener:
        print(event.type, event.payload)


if __name__ == "__main__":
    asyncio.run(main())
