from imports import *
from urllib.parse import urlparse, parse_qs
import os
import mimetypes

YOUTUBE_HOSTS = {"youtube.com", "www.youtube.com", "youtu.be"}
VIMEO_HOSTS = {"vimeo.com", "www.vimeo.com"}
TIKTOK_HOSTS = {"tiktok.com", "www.tiktok.com"}
TWITTER_HOSTS = {"twitter.com", "x.com", "www.twitter.com", "www.x.com"}
DIRECT_EXTS = {
    ".png", ".jpg", ".jpeg", ".gif", ".webp", ".svg",
    ".mp4", ".mov", ".mkv", ".avi",
    ".mp3", ".wav", ".ogg",
    ".pdf", ".zip", ".rar", ".7z", ".gz",
}

def _clean_url(url: str) -> str:
    """ normalize scheme + strip weird fragments """
    if not url:
        return ""
    url = url.strip()
    if url.startswith("//"):
        url = "https:" + url
    return url

# -----------------------------------------------------
# PLATFORM EXTRACTORS
# -----------------------------------------------------

def detect_youtube(parsed, query):
    netloc = parsed.netloc.lower()
    if netloc not in YOUTUBE_HOSTS:
        return None

    # video id
    if "v" in query:
        return {
            "downloadable": True,
            "kind": "video",
            "provider": "youtube",
            "id": query.get("v", [None])[0],
            "direct": False,
        }

    # youtu.be short links
    if parsed.netloc == "youtu.be":
        vid = parsed.path.strip("/")
        return {
            "downloadable": True,
            "kind": "video",
            "provider": "youtube",
            "id": vid,
            "direct": False,
        }

    # playlists
    if "list" in query:
        return {
            "downloadable": True,
            "kind": "playlist",
            "provider": "youtube",
            "id": query.get("list", [None])[0],
            "direct": False,
        }

    return {
        "downloadable": False,
        "kind": None,
        "provider": "youtube",
        "id": None,
        "direct": False,
    }


def detect_vimeo(parsed):
    if parsed.netloc.lower() not in VIMEO_HOSTS:
        return None
    vid = parsed.path.strip("/")
    if vid.isdigit():
        return {
            "downloadable": True,
            "kind": "video",
            "provider": "vimeo",
            "id": vid,
            "direct": False
        }
    return {"downloadable": False, "provider": "vimeo"}


def detect_tiktok(parsed):
    if parsed.netloc.lower() not in TIKTOK_HOSTS:
        return None

    parts = parsed.path.split("/")
    # /@user/video/123456
    if "video" in parts:
        idx = parts.index("video")
        vid = parts[idx + 1] if idx + 1 < len(parts) else None
        return {
            "downloadable": True,
            "kind": "video",
            "provider": "tiktok",
            "id": vid,
            "direct": False,
        }
    return {"downloadable": False, "provider": "tiktok"}


def detect_twitter(parsed, query):
    if parsed.netloc.lower() not in TWITTER_HOSTS:
        return None

    parts = parsed.path.split("/")
    # /username/status/ID
    if "status" in parts:
        idx = parts.index("status")
        tid = parts[idx + 1] if idx + 1 < len(parts) else None
        return {
            "downloadable": True,
            "kind": "video",
            "provider": "twitter",
            "id": tid,
            "direct": False,
        }
    return {"downloadable": False, "provider": "twitter"}


# -----------------------------------------------------
# DIRECT FILE DETECTION
# -----------------------------------------------------

def detect_direct_file(parsed):
    ext = os.path.splitext(parsed.path)[1].lower()
    if ext in DIRECT_EXTS:
        return {
            "downloadable": True,
            "kind": mimetypes.guess_type(parsed.path)[0] or "file",
            "provider": None,
            "id": None,
            "direct": True,
        }
    return None


# -----------------------------------------------------
# MAIN ENTRY POINT
# -----------------------------------------------------

def get_downloadable_info(url: str):
    url = _clean_url(url)
    parsed = urlparse(url)
    query = parse_qs(parsed.query)

    # 1. Direct file
    direct = detect_direct_file(parsed)
    if direct:
        direct["url"] = url
        return direct

    # 2. YouTube
    yt = detect_youtube(parsed, query)
    if yt:
        yt["url"] = url
        return yt

    # 3. Vimeo
    vimeo = detect_vimeo(parsed)
    if vimeo:
        vimeo["url"] = url
        return vimeo

    # 4. TikTok
    tiktok = detect_tiktok(parsed)
    if tiktok:
        tiktok["url"] = url
        return tiktok

    # 5. Twitter/X
    tw = detect_twitter(parsed, query)
    if tw:
        tw["url"] = url
        return tw

    # fallback
    return {
        "downloadable": False,
        "kind": None,
        "provider": None,
        "id": None,
        "direct": False,
        "url": url,
    }

video_url = "https://www.youtube.com/watch?v=mIMMZQJ1H6E&list=RDmIMMZQJ1H6E&start_radio=https://www.youtube.com/watch?v=SeqaTKZOCRs"
input(get_downloadable_info(url=video_url))
corrected_url= get_corrected_url(video_url)
url_mgr = urlManager(video_url)
name = url_mgr.parsed.get('name')
name = url_mgr.parsed.get('name')
domain = url_mgr.parsed.get('domain')
query = url_mgr.parsed.get('query')
v = query.get('v')
if name == 'youtube':
    video_url = f'https://www.{domain}/watch?v={v}'
input(get_youtube_parsed_dict(url=video_url))
url_mgr = urlManager(DOMAIN)



