"""Bernard Temporal worker entrypoint."""
from __future__ import annotations

import asyncio
import os

from temporalio.client import Client
from temporalio.worker import Worker

from bernard.temporal import activities
from bernard.temporal.workflows import PRReviewWorkflow, TemporalMonitorWorkflow


async def _main() -> None:
    address = os.environ.get("TEMPORAL_ADDRESS", "temporal-frontend.ai-core.svc.cluster.local:7233")
    namespace = os.environ.get("TEMPORAL_NAMESPACE", "default")
    task_queue = os.environ.get("BERNARD_TASK_QUEUE", "bernard")

    client = await Client.connect(address, namespace=namespace)
    worker = Worker(
        client,
        task_queue=task_queue,
        workflows=[PRReviewWorkflow, TemporalMonitorWorkflow],
        activities=[
            activities.fetch_open_prs,
            activities.fetch_pr_diff,
            activities.review_pr_with_llm,
            activities.post_pr_comment,
            activities.get_reviewed_prs,
            activities.mark_pr_reviewed,
            activities.fetch_failed_workflows,
            activities.fetch_workflow_failure_details,
            activities.analyze_failure_with_llm,
            activities.persist_alert,
        ],
    )
    print(f"Bernard worker started on task queue '{task_queue}'")
    await worker.run()


def main() -> None:
    asyncio.run(_main())


if __name__ == "__main__":
    main()
