# faststream run faststream_app:app

import asyncio
import os
import time
from cProfile import run

from faststream import FastStream
from faststream.specification.asyncapi.v3_0_0.schema import servers
from pydantic import BaseModel, Field
from faststream.nats import NatsBroker, JStream, PullSub
from urllib3.util import url

NATS_SERVERS = os.getenv('NATS_URL', 'nats://localhost:4222').split(',')
NATS_TOKEN = os.getenv('NATS_TOKEN', '')

broker = NatsBroker(servers=NATS_SERVERS, token=NATS_TOKEN)
app = FastStream(broker)

stream = JStream(name="orders", subjects=["orders.*"])

@broker.subscriber("orders.created",
                   stream=stream,
                   #pull_sub=PullSub(),
                   queue="orders-queue",
                   #durable="orders-worker"
                   ) #event-processor
async def handle_order(message: dict):
    print("received:", message)
    return {"status": "ok"}

# @broker.publisher("orders.created", stream="orders")
# async def publish_order():
#     return {"order_id": 123, "item": "book"}


async def main():
    #await publish_order()
    await app.run()

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