
from abstract_webtools import *
from .imports import *
# one call, one object, passed everywhere — no module-level globals
config = AppConfig.from_env()
MAX_FITZ_PAGES = 2000   # safety ceiling — don't OCR enormous PDFs without explicit override
def path_to_url(path,root_url=None,media_root=None):
    media_root = media_root or MEDIA_ROOT
    root_url = root_url or ROOT_URL
    return path.replace(media_root,root_url)
def url_to_path(path,root_url=None,media_root=None):
    media_root = media_root or MEDIA_ROOT
    root_url = root_url or ROOT_URL
    return path.replace(root_url,media_root)
def get_parsed_url(domain, **kwargs):
    parsed_url = dict(kwargs)
    post_variants = []
    # http / www
    http_www = get_http_www(domain)
    parsed_url.update(http_www)
    http = http_www.get('http')
    # basic domain pieces
    domain_paths = get_domain_paths(domain, http=http)
    if 'path' not in parsed_url:
        parsed_url['path']=[]
    parsed_url['path']+=domain_paths
    domain_name_ext = get_domain_name_ext(domain, http=http)
    parsed_url.update(domain_name_ext)

    domain_name = parsed_url.get('name',"") or ""
    domain = parsed_url.get('domain',"") or ""
        # tokenization
    tokenized_domain = tokenize_domain(domain)
    parsed_url["tokenized_domain"] = tokenized_domain
    app_name = " ".join(tokenized_domain)
    
    parsed_url["app_name"] = app_name
    # author / "i_url"
    parsed_url["author"] = f"@{domain_name.lower()}"
    parsed_url["i_url"] = f"{domain_name}://"

    # combine with domain
    # compute final title
    title = get_title(parsed_url)
    
    post_variants=[title,app_name,domain]
    variants = title_variants_from_domain(domain)
    base_variants = list(set([variant for variant in variants if variant not in post_variants]))
    # update the organized variants
    parsed_url["title_variants"] = get_all_title_variants(variants=base_variants,page=title,name=app_name,domain=domain)

    parsed_url["title"] = pad_or_trim(
        "title",
        string=title,
        title_variants=parsed_url["title_variants"],
        page=title,
        domain=domain,
        name = app_name
    )
    # get keywords
    keywords_info = get_keywords(parsed_url,page=title,domain=domain,name = app_name)
    parsed_url.update(keywords_info)
    keywords = parsed_url.get("keywords", [])
    # FINAL: longest→shortest list with TITLE first, DOMAIN second
    domain = parsed_url.get("domain")
    if domain:
        final_variants = [title,page_data ]
        # remove title/domain from pool
        pool = set(keywords + variants)
        pool.discard(title)
        pool.discard(parsed_url.get("domain"))

        # sort longest → shortest
        final_variants += sort_longest_first(pool)

        parsed_url["title_variants"] = final_variants
   
    return parsed_url
def init_page_data(config):
    page_data = {}
    ROOT_URL = config.root_url
    MEDIA_ROOT = config.media_root
    SITE_NAME = config.site_name
    DOMAIN = config.domain
    variants=title_variants_from_domain(DOMAIN)
    title_variants=title_variants_from_domain(DOMAIN) 
    site_name=SITE_NAME
    domain=DOMAIN
    root_url=ROOT_URL
    page_data = getInfo(domain=DOMAIN)
    page_data = get_parsed_url(**page_data)
    page_data['domain']=domain
    page_data['variants']=variants
    page_data['site'] = root_url
    page_data['site_name'] = site_name
    page_data['creator'] = site_name
    page_data['author'] = f'@{site_name}'
    return page_data

def get_page_data(
    title,
    href,
    summary,
    keywords,
    keywords_str,
    thumbnail,
    alt=None,
    caption=None,
    ):
    config = SiteConfig.from_env()
    ROOT_URL = config.root_url
    MEDIA_ROOT = config.media_root
    thumbnail_url=path_to_url(thumbnail,ROOT_URL,MEDIA_ROOT)
    page_data = init_page_data(config)
    page_data["title"] = title
    page_data['href'] = href
    page_data["page_url"] = href
    page_data['share_url'] = href
    page_data['thumbnail'] = thumbnail
    page_data['thumbnail_link'] = thumbnail_url
    page_data['description'] = summary
    page_data['keywords'] = keywords
    page_data['keywords_str']= ','.join(keywords)
    page_data["alt"] = alt or title
    page_data["caption"] = caption or summary
    meta_info = get_meta_info(info=page_data,base_image_dir=MEDIA_ROOT,base_image_url=ROOT_URL)
    meta_info['thumbnail_resized'] = path_to_url(meta_info.get('thumbnail_resized',thumbnail_url) or thumbnail_url,media_root=MEDIA_ROOT,root_url=ROOT_URL)
    meta_info['thumbnail_url_resized'] = path_to_url(meta_info.get('thumbnail_url_resized',thumbnail_url) or thumbnail_url,media_root=MEDIA_ROOT,root_url=ROOT_URL)
    meta_info['thumbnail'] = path_to_url(meta_info.get('thumbnail',thumbnail_url) or thumbnail_url,media_root=MEDIA_ROOT,root_url=ROOT_URL)
    meta_info['og']['image'] = path_to_url(meta_info.get('og',{}).get('image',thumbnail_url) or thumbnail_url,media_root=MEDIA_ROOT,root_url=ROOT_URL)
    meta_info['twitter']['image'] = path_to_url(meta_info.get('twitter',{}).get('image',thumbnail_url) or thumbnail_url,media_root=MEDIA_ROOT,root_url=ROOT_URL)

    return meta_info
##    page_data["alt"] = alt or title
##    page_data["caption"] = caption or summary
##    page_data["schema"] = meta.get("og", {})
##    page_data["schema"]['site_name'] = SITE_NAME
##    page_data["social_meta"] = meta.get("twitter", {})
##    page_data["other"] = meta.get("other")
##    page_data['meta']= meta
##    return page_data
