"""Tests for restate_saga.restate_workflows."""

import restate

from restate_saga.restate_workflows import SagaRestateWorkflow, create_saga_restate_workflow
from restate_saga.types import SagaWorkflowOptions, WorkflowRetryPolicy


# ---------------------------------------------------------------------------
# create_saga_restate_workflow
# ---------------------------------------------------------------------------


def test_create_basic():
    wf = create_saga_restate_workflow("TestWf")
    assert isinstance(wf, SagaRestateWorkflow)
    assert isinstance(wf, restate.Workflow)
    assert wf.name == "TestWf"


def test_create_with_options():
    opts = SagaWorkflowOptions(
        retry_policy=WorkflowRetryPolicy(max_attempts=3),
    )
    wf = create_saga_restate_workflow("TestWf", options=opts)
    assert wf.name == "TestWf"


# ---------------------------------------------------------------------------
# Handler registration
# ---------------------------------------------------------------------------


def test_main_handler_registered():
    wf = create_saga_restate_workflow("MainWf")

    @wf.main()
    async def run(saga, ctx, input_val):
        return {"ok": True}

    handler_names = list(wf.handlers)
    assert "run" in handler_names


def test_shared_handler_registered():
    wf = create_saga_restate_workflow("SharedWf")

    @wf.main()
    async def run(saga, ctx, input_val):
        return input_val

    @wf.handler()
    async def get_status(ctx):
        return "ok"

    handler_names = list(wf.handlers)
    assert "run" in handler_names
    assert "get_status" in handler_names


def test_main_sets_saga_handler():
    wf = create_saga_restate_workflow("HandlerWf")

    @wf.main()
    async def run(saga, ctx, input_val):
        return input_val

    assert wf._saga_handler is not None
    assert wf._saga_input_model is None  # no pydantic model


# ---------------------------------------------------------------------------
# isinstance checks (important for restate.app())
# ---------------------------------------------------------------------------


def test_isinstance_workflow():
    wf = create_saga_restate_workflow("CheckWf")
    assert isinstance(wf, restate.Workflow)


# ---------------------------------------------------------------------------
# Subclass behavior
# ---------------------------------------------------------------------------


def test_saga_restate_workflow_is_subclass():
    assert issubclass(SagaRestateWorkflow, restate.Workflow)


# ---------------------------------------------------------------------------
# run_as_step requires main handler
# ---------------------------------------------------------------------------


def test_run_as_step_raises_without_main():
    import pytest

    wf = create_saga_restate_workflow("NoMainWf")
    saga = None  # won't get that far

    with pytest.raises(RuntimeError, match="no @main handler"):
        import asyncio
        asyncio.get_event_loop().run_until_complete(wf.run_as_step(saga, {}))


# ---------------------------------------------------------------------------
# repr
# ---------------------------------------------------------------------------


def test_repr():
    wf = create_saga_restate_workflow("MyWorkflow")
    assert repr(wf) == "SagaRestateWorkflow('MyWorkflow')"
