import re
from bbot.modules.base import BaseModule


class social(BaseModule):
    watched_events = ["URL_UNVERIFIED"]
    produced_events = ["SOCIAL"]
    meta = {"description": "Look for social media links in webpages"}
    flags = ["passive", "safe", "social-enum"]

    social_media_regex = {
        "linkedin": r"(?:https?://)?(?:www.)?linkedin.com/(?:in|company)/([a-zA-Z0-9-]+)/?",
        "facebook": r"(?:https?://)?(?:www.)?facebook.com/([a-zA-Z0-9.]+)/?",
        "twitter": r"(?:https?://)?(?:www.)?twitter.com/([a-zA-Z0-9_]{1,15})/?",
        "github": r"(?:https?://)?(?:www.)?github.com/([a-zA-Z0-9_-]+)/?",
        "instagram": r"(?:https?://)?(?:www.)?instagram.com/([a-zA-Z0-9_.]+)/?",
        "youtube": r"(?:https?://)?(?:www.)?youtube.com/@([a-zA-Z0-9_]+)/?",
        "bitbucket": r"(?:https?://)?(?:www.)?bitbucket.org/([a-zA-Z0-9_-]+)/?",
        "gitlab": r"(?:https?://)?(?:www.)?gitlab.(?:com|org)/([a-zA-Z0-9_-]+)",
        "discord": r"(?:https?://)?(?:www.)?discord.gg/([a-zA-Z0-9_-]+)",
        "docker": r"(?:https?://)?hub.docker.com/[ru]/([a-zA-Z0-9_-]+)",
        "huggingface": r"(?:https?://)?huggingface.co/([a-zA-Z0-9_-]+)",
    }

    scope_distance_modifier = 1

    async def setup(self):
        self.compiled_regexes = {k: re.compile(v) for k, v in self.social_media_regex.items()}
        return True

    async def handle_event(self, event):
        for platform, regex in self.compiled_regexes.items():
            for match in regex.finditer(event.data):
                url = match.group()
                if not url.startswith("http"):
                    url = f"https://{url}"
                profile_name = match.groups()[0]
                social_event = self.make_event(
                    {"platform": platform, "url": url, "profile_name": profile_name}, "SOCIAL", source=event
                )
                social_event.scope_distance = event.scope_distance
                await self.emit_event(social_event)
