import builtins
from typing import Dict

from pydantic import BaseModel, Field
from tldextract import extract

from app import Proxy


class MMMirror(BaseModel):
    origin_domain: str = Field(description="The full origin domain name")
    origin_domain_normalized: str = Field(description="The origin_domain with \"www.\" removed, if present")
    origin_domain_root: str = Field(description="The registered domain name of the origin, excluding subdomains")


class MirrorMapping(BaseModel):
    __root__: Dict[str, MMMirror] = Field(
        description="The domain name for the mirror"
    )

    class Config:
        title = "Mirror Mapping Version 1"


def mirror_mapping():
    return MirrorMapping(**{
        d.url.lstrip("https://"): MMMirror(
            origin_domain=d.origin.domain_name,
            origin_domain_normalized=d.origin.domain_name.lstrip("www."),
            origin_domain_root=extract(d.origin.domain_name).registered_domain
        ) for d in Proxy.query.all() if d.url is not None
    }).dict()


if getattr(builtins, "__sphinx_build__", False):
    schema = MirrorMapping.schema_json()
