from __future__ import annotations

from datetime import timedelta

from temporalio import workflow

with workflow.unsafe.imports_passed_through():
    from gumbo.temporal.activities import persist_gumbo_summary, run_gumbo_langgraph_job


@workflow.defn
class GumboSummarizeWorkflow:
    """Orchestrates Gumbo: LangGraph run (MCP + LiteLLM + Postgres checkpoints) then durable write."""

    @workflow.run
    async def run(self, object_key: str) -> str:
        wf_id = workflow.info().workflow_id
        params = {"object_key": object_key, "thread_id": wf_id}
        out = await workflow.execute_activity(
            run_gumbo_langgraph_job,
            params,
            start_to_close_timeout=timedelta(minutes=30),
        )
        await workflow.execute_activity(
            persist_gumbo_summary,
            {
                "workflow_id": wf_id,
                "object_key": out["object_key"],
                "summary": out["summary"],
            },
            start_to_close_timeout=timedelta(minutes=5),
        )
        return out["summary"]


@workflow.defn
class GumboApprovalGateWorkflow:
    """Example pattern: wait for an external approval signal before continuing."""

    def __init__(self) -> None:
        self._approved = False

    @workflow.signal
    def approve(self) -> None:
        self._approved = True

    @workflow.run
    async def run(self) -> str:
        await workflow.wait_condition(lambda: self._approved)
        return "approved"
