import asyncio
import base64
import json
import os

from dotenv import load_dotenv

from mtmaisdk import new_client
from mtmaisdk.clients.admin import TriggerWorkflowOptions
from mtmaisdk.clients.rest.models.workflow_run import WorkflowRun
from mtmaisdk.clients.run_event_listener import StepRunEventType


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

    workflowRuns: WorkflowRun = []

    # we are going to run the BulkParent workflow 20 which will trigger the Child workflows n times for each n in range(20)
    for i in range(20):
        workflowRuns.append(
            {
                "workflow_name": "BulkParent",
                "input": {"n": i},
                "options": {
                    "additional_metadata": {
                        "bulk-trigger": i,
                        "hello-{i}": "earth-{i}",
                    },
                },
            }
        )

    workflowRunRefs = hatchet.admin.run_workflows(
        workflowRuns,
    )

    results = await asyncio.gather(
        *[workflowRunRef.result() for workflowRunRef in workflowRunRefs],
        return_exceptions=True,
    )

    for result in results:
        if isinstance(result, Exception):
            print(f"An error occurred: {result}")  # Handle the exception here
        else:
            print(result)


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