import unittest
from msgprocessor import (
    TrackerRemovalMsgProcessor,
    TrackerRemoverFactory,
    TrackerRemovalProcessorMessage,
    TrackerRemovalResult,
)
from dataclasses import dataclass


@dataclass(init=True, eq=True)
class TestUser:
    id: int
    first_name: str


class TestRemoverFactory(unittest.TestCase):
    factory = TrackerRemoverFactory()

    def test_remove_strategy_constructor(self):
        test_case_data = [
            {"domain": "youtube.com", "remover": self.factory.remove_yt_trackers},
            {
                "domain": "lowerlevel.youtube.com",
                "remover": self.factory.remove_yt_trackers,
            },
            {
                "domain": "youtu.be",
                "remover": self.factory.remove_yt_trackers,
            },
            {
                "domain": "something.youtu.be",
                "remover": self.factory.remove_yt_trackers,
            },
        ]
        for test_case in test_case_data:
            self.assertIs(
                self.factory.make_remover(test_case["domain"]),
                self.factory.remove_yt_trackers,
            )

    def test_remove_yt_si(self):
        test_case_data = [
            {
                "url": "https://youtu.be/jNQXAC9IVRw?si=qLIZT1rvs99_jbgy",
                "expected_url": "https://youtu.be/jNQXAC9IVRw",
            },
            {
                "url": "https://youtu.be/jNQXAC9IVRw?si=qLIZT1rvs99_jbgy&t=16",
                "expected_url": "https://youtu.be/jNQXAC9IVRw?t=16",
            },
            {
                "url": "https://www.youtube.com/watch?v=jNQXAC9IVRw",
                "expected_url": "https://www.youtube.com/watch?v=jNQXAC9IVRw",
            },
            {
                "url": "http://www.youtube.com/watch?v=jNQXAC9IVRw&si=qLIZT1rvs99_jbgy&t=16",
                "expected_url": "http://www.youtube.com/watch?v=jNQXAC9IVRw&t=16",
            },
        ]
        for test_case in test_case_data:
            self.assertEqual(
                self.factory.remove_yt_trackers(test_case["url"]),
                test_case["expected_url"],
            )


class TestRemovalMsgProcessor(unittest.TestCase):
    def test_remove_links(self):
        test_case_data = [
            {
                "msg_text": "https://youtu.be/jNQXAC9IVRw?si=qLIZT1rvs99_jbgy",
                "sender": TestUser(id=123, first_name="Ghytro"),
                "bot_responded": True,
                "bot_response": 'Message from <a href="tg://user?id=123">Ghytro</a>:\n\nhttps://youtu.be/jNQXAC9IVRw',
            },
            {
                "msg_text": "чекай https://youtu.be/jNQXAC9IVRw?si=qLIZT1rvs99_jbgy\nнаш слон хд",
                "sender": TestUser(id=321, first_name="OllyHearn"),
                "bot_responded": True,
                "bot_response": 'Message from <a href="tg://user?id=321">OllyHearn</a>:\n\nчекай https://youtu.be/jNQXAC9IVRw\nнаш слон хд',
            },
            {
                "msg_text": "а я такая нитакуся без si ссылки шлю сразу https://youtu.be/jNQXAC9IVRw и по нескольку штук\nhttp://www.youtube.com/watch?v=jNQXAC9IVRw&si=qLIZT1rvs99_jbgy&t=16 дада",
                "sender": TestUser(id=321, first_name="OllyHearn"),
                "bot_responded": True,
                "bot_response": 'Message from <a href="tg://user?id=321">OllyHearn</a>:\n\nа я такая нитакуся без si ссылки шлю сразу https://youtu.be/jNQXAC9IVRw и по нескольку штук\nhttp://www.youtube.com/watch?v=jNQXAC9IVRw&t=16 дада',
            },
            {
                "msg_text": "asdasdasdasdasdasdasd asdasd asdasd asdad sasa dadsas",
                "sender": TestUser(id=123, first_name="Ghytro"),
                "bot_responded": False,
                "bot_response": "",
            },
        ]
        for test_case in test_case_data:
            result = TrackerRemovalMsgProcessor(
                TrackerRemovalProcessorMessage(
                    fromUser=test_case["sender"], text=test_case["msg_text"]
                )
            ).process()
            self.assertEqual(
                result,
                TrackerRemovalResult(
                    needsToReply=test_case["bot_responded"],
                    text=test_case["bot_response"],
                ),
            )


if __name__ == "__main__":
    test_classes_to_run = [TestRemoverFactory, TestRemovalMsgProcessor]

    loader = unittest.TestLoader()

    suites_list = []
    for test_class in test_classes_to_run:
        suite = loader.loadTestsFromTestCase(test_class)
        suites_list.append(suite)

    big_suite = unittest.TestSuite(suites_list)

    runner = unittest.TextTestRunner()
    results = runner.run(big_suite)
