from dataclasses import dataclass
from enum import Enum

from rgltr.context import context, restore_tags_from_messages
from rgltr.decorators import tag


class Tag(Enum):
    A = "a"


@tag(activates=[Tag.A])
async def tool_a() -> str:
    return "ok"


@dataclass
class FakePart:
    tool_name: str


@dataclass
class FakeMessage:
    parts: list[FakePart]


def test_nested_context_inherits_tags():
    with context() as outer:
        outer.activate({Tag.A})
        with context() as inner:
            assert Tag.A in inner.active_tags


def test_restore_tags_from_messages():
    messages = [FakeMessage(parts=[FakePart(tool_name="tool_a")])]

    def lookup(name: str):
        if name == "tool_a":
            return {Tag.A}
        return None

    with context() as ctx:
        restore_tags_from_messages(messages, lookup, target=ctx)
        assert Tag.A in ctx.active_tags
