from fnmatch import fnmatch
from typing import Tuple, List

import requests

from app.extensions import db
from app.models.activity import Activity
from app.models.mirrors import Proxy
from app.terraform import BaseAutomation


class BlockRoskomsvobodaAutomation(BaseAutomation):
    short_name = "block_roskomsvoboda"
    description = "Import Russian blocklist from RosKomSvoboda"
    frequency = 90

    def automate(self, full: bool = False) -> Tuple[bool, str]:
        activities = []
        proxies: List[Proxy] = Proxy.query.filter(
            Proxy.deprecated == None,
            Proxy.destroyed == None
        ).all()
        patterns = requests.get("https://reestr.rublacklist.net/api/v2/domains/json").json()
        for pattern in patterns:
            for p in proxies:
                if fnmatch(p.url[len("https://"):], pattern):
                    print(f"Found {p.url} blocked")
                    if not p.origin.auto_rotation:
                        print("Proxy auto-rotation forbidden for origin")
                        continue
                    if p.deprecated:
                        print("Proxy already marked blocked")
                        continue
                    p.deprecate(reason="roskomsvoboda")
                    activities.append(Activity(
                        activity_type="block",
                        text=(f"Proxy {p.url} for {p.origin.domain_name} detected blocked according to RosKomSvoboda. "
                              "Rotation scheduled.")
                    ))
        for a in activities:
            db.session.add(a)
        db.session.commit()
        for a in activities:
            a.notify()
        return True, ""
