import context
import a2a_acl.message_tools.acl_message

import unittest
import uuid
from typing import Any

from a2a.server.agent_execution import RequestContext
from a2a.types import Message, Role, Part, TextPart, MessageSendParams

from a2a_acl.protocol_extension import MIME_TAG
from a2a_acl.protocol_extension.primitives import (
    check_performative_regexpr,
    add_performative,
    Performative,
    extract_performative,
)

URI = "https://mosaico-project.eu/extensions/a2a-acl/"


class TestACLtoA2A(unittest.TestCase):
    def test_performative_in_message(self):
        md: dict[str, Any] = {
            "performative": "inform",
            "codec": "nl",
            "mediaType": MIME_TAG,
        }
        m = Message(
            role=Role.agent,
            message_id=uuid.uuid4().hex,
            parts=[Part(root=TextPart(text="I am ready."))],
            extensions=[URI],
            metadata={URI: md},
        )
        p = MessageSendParams(message=m)
        c = RequestContext(request=p)
        self.assertEqual(
            a2a_acl.message_tools.acl_message.acl_of_a2a(c).performative, "inform"
        )

    def test_performative_in_message2(self):
        md: dict[str, Any] = {
            "performative": "inform",
            "codec": "nl",
            "mediaType": MIME_TAG,
        }
        m = Message(
            role=Role.agent,
            message_id=uuid.uuid4().hex,
            parts=[Part(root=TextPart(text="I am ready."))],
            extensions=[URI],
            metadata={URI: md},
        )
        p = Performative("accept-proposal")
        add_performative(m, p, "nl")

        self.assertEqual(m.metadata[URI]["performative"], p)

    def test_performative_regexpr(self):
        self.assertTrue(check_performative_regexpr("inform"))
        self.assertTrue(check_performative_regexpr("not-understood"))
        self.assertFalse(check_performative_regexpr("not ready"))
        self.assertFalse(check_performative_regexpr("not_ready"))
        self.assertFalse(check_performative_regexpr("-not-ready"))
        self.assertFalse(check_performative_regexpr("Ready"))
        self.assertFalse(check_performative_regexpr(" ready"))
        self.assertFalse(check_performative_regexpr(""))
        self.assertFalse(check_performative_regexpr("ready1"))


if __name__ == "__main__":
    unittest.main()
