"""Stream URL extractors per-tube.

Public API:
  - `try_extract(sitetag, page_url) -> list[StreamSource] | None`
  - `StreamSource` (dataclass)
  - `HosterDead` (exception)
  - `extract_stream_from_hoster(iframe_url, *, referer)` — generic packer-based hoster extract
  - `fetch_tube_html(url)` — Chrome TLS fingerprint fetch (curl_cffi)
  - `browser_get(url)` — low-level

Architektura: każdy tube ma osobny moduł `app.extractors.tubes.<tube>` który eksportuje
`extract(page_url) -> list[StreamSource] | None`. Registry niżej mapuje sitetag →
modułowy extractor. `try_extract()` to thin wrapper z exception handlingiem.

Po removalu porn-app dependency, ten moduł jest jedynym mechanizmem rozwiązywania
streamów — playback.py nie wpada już do porn-app /stream API.
"""
from __future__ import annotations

import logging
from collections.abc import Callable

from app.extractors._fetch import browser_get, fetch_tube_html
from app.extractors._models import HosterDead, StreamSource, TubePageError
from app.extractors.hoster import extract_stream_from_hoster, unpack_packer
from app.extractors.tubes import (
    _embed_iframe,
    _vps_blocked_fallback,
    _ytdlp,
    eporner,
    freshporno,
    hqporner,
    latestpornvideo,
    paradisehill,
    porn00,
    pornhat,
    porntrex,
    pornxp,
    sxyprn,
    yespornvip,
)

log = logging.getLogger(__name__)


# Sitetag → extractor function. Sitetag pasuje do format'u z origin: `pornapp:<sitetag>`
# (lub po Fazie 2 migracji: `tube:<sitetag>`).
#
# Mainstream tubes (pornhub/xvideos/xnxx/xhamster/redtube/youporn/porntrex) używają
# yt-dlp jako extractor — battle-tested, aktualizowane przez upstream przy zmianach
# HTML. Aggregator tubes (xmoviesforyou/watchporn/siska/...) używają generic
# embed-iframe extractor (page → /e/<id> iframe → P.A.C.K.E.R. unpack). Custom kod
# tylko tam gdzie tube ma niestandardowy schemat (eporner XHR, sxyprn URL transform).
_REGISTRY: dict[str, Callable[[str], list[StreamSource] | None]] = {
    # hqporner — dedicated extractor zwraca multi-quality `<source>` mp4 URLs
    # (bigcdn.cc / hqwo.cc / flyflv) z `force_proxy=True`. CDN URLs IP-bound do
    # VPS, więc playback.py routuje przez proxy — mobile dostaje quality picker
    # + natywny ExoPlayer, bez WebView.
    # Bug-report e8ddd8d4: WebView fallback (`_vps_blocked_fallback`) ładował
    # hqporner.com scene page w WebView, ale ta strona ma ad-iframes (adtng,
    # goaserv, mavrtracktor) + pop-under-triggery → user klikał i widział
    # reklamę zamiast video. INJECTED_JS w PlayerScreen.tsx nie chwytał
    # popupów dośc szybko. Powrót do natywnego = `<source>` mp4 picker omija
    # tę ścieżkę całkowicie.
    "hqpornercom": hqporner.extract,
    "epornercom": eporner.extract,
    "sxyprncom": sxyprn.extract,
    # Mainstream tubes — yt-dlp
    # NB: 2026-05-18 cross-IP test potwierdził że xvideos/xnxx/pornhub/youporn/redtube
    # CDN URLs są **time-bound** (nie IP-bound) — mobile_direct_ok auto-detect w
    # playback.py daje mobile direct fetch, zero VPS bandwidth.
    "pornhubcom": _ytdlp.extract,
    "redtubecom": _ytdlp.extract,
    "xvideoscom": _ytdlp.extract,
    "xnxxcom": _ytdlp.extract,
    "youporncom": _ytdlp.extract,
    # porntrex KVS — 2026-05-22 VPS znów dociera (HTTP 200). Dedykowany extractor:
    # flashvars `video_url` → `get_file` 302 → CDN time-bound signed URL
    # (`expires`+`md5`, NIE IP-bound) → mobile gra direct, zero VPS bandwidth.
    "porntrexcom": porntrex.extract,
    # VPS-blocked tubes — KVS / Cloudflare blokuje Hetzner IP, ale działają z residential
    # IP (potwierdzone Chrome DevTools MCP 2026-05-15). Mobile WebView + INJECTED_JS
    # (PlayerScreen.tsx:805) skanuje <video>.src + XHR — łapie URL po decode-ie player JS.
    "xhamstercom": _vps_blocked_fallback.extract,
    "porndittcom": _vps_blocked_fallback.extract,
    "fpoxxx": _vps_blocked_fallback.extract,
    "sxylandcom": _vps_blocked_fallback.extract,
    # Aggregator tubes — generic embed-iframe → hoster unpacker
    "latestpornvideocom": latestpornvideo.extract,
    "xmoviesforyoucom": _embed_iframe.extract,
    "watchporn": _embed_iframe.extract,
    "siskavideo": _embed_iframe.extract,
    "porn4dayspw": _embed_iframe.extract,
    "porndishcom": _embed_iframe.extract,
    # xxxfreewatch — DELISTED 2026-05-18. 790 solo-orphan scen, 0% match, CF-walled z VPS.
    "latestleaksco": _embed_iframe.extract,
    "mypornerleakcom": _embed_iframe.extract,
    # PornHat — dedicated extractor: tylko `<source>` z player area (skip sidebar
    # trailer URLs `_preview*.mp4`), dedupe po filename. Get_file 302 → CDN, proxy
    # follow_redirects=True wymagane (fix w stream_proxy.py).
    "pornhatcom": pornhat.extract,
    # Freshporno KVS — `cv=` HMAC signed token IP-bound do VPS. WebView fallback:
    # mobile fetchuje embed z phone IP, KVS player JS dekoduje video_url, ExoPlayer
    # odtwarza direct z CDN. UX trade-off (page flicker przed video) vs bandwidth/
    # anonimowość — public-app priorytet → WebView wygrywa.
    # (2026-05-28: krótko-żywy switch na freshporno.extract z force_proxy=True
    # cofnięty po feedbacku Jana "video proxy mnie nie interesuje, idziemy
    # publicznie".)
    "freshpornoorg": _vps_blocked_fallback.extract,
    # porn00 — KVS engine z v-acctoken w URL. Backend extract działa (zweryfikowane
    # 2026-05-23), zwraca świeże get_file URL-e z `force_proxy=True` flag.
    # `_proxify_link` rozwija je przez VPS proxy (CDN token IP-bound do VPS, mobile
    # direct = 403). Bug-reports `5037b3e3`/`e8e3198b` 2026-05-22: WebView fallback
    # pokazywał reklamę full-screen (porn00.org ma agresywny ad-network) — mobile
    # nigdy nie dochodził do `<video>` tag dla INJECTED_JS scrape. Z fresh extract
    # mobile dostaje proxy URL od razu, ExoPlayer gra bez WebView.
    "porn00org": porn00.extract,
    "pornxpph": _vps_blocked_fallback.extract,
    # yesporn.vip — KVS engine. flashvars `video_url: 'function/0/<get_file_url>'`,
    # function/0 to passthrough wrapper, URL po prefixie direct streamuje (480p).
    # Wymagało odrębnego extractora bo origin `tube:yespornvip` był w
    # playback_sources ale brak handlera w _REGISTRY → mobile no-source (bug-report
    # 2026-05-27 "Yespornvip dalej nie działa").
    "yespornvip": yespornvip.extract,
    # Direct-scraping tubes (mają też search scraper w connectors/direct_scrapers/)
    # — używają identycznego embed-iframe pattern dla streamingu.
    # hdporn92com — DELISTED 2026-05-18. Scene pages to SEO shell bez player iframe,
    # JS hijackuje kliki na popunder. Wszystkie playback_sources mass-marked dead.
    # 0dayxx wraps watchporn.to embed. watchporn.to/get_file/ token IP-bound (302→410
    # cross-IP). Switch na WebView fallback. ~5k scen.
    "0dayxxcom": _vps_blocked_fallback.extract,
    # CF-protected tube — curl_cffi w fetch_tube_html bypassa JA3, embed-iframe pattern.
    "perverzijacom": _embed_iframe.extract,
    # Special: WebView-only (Yii2 session-bound player).
    "paradisehillcc": paradisehill.extract,
    # PornDoe — dołączony 2026-05-21 (theporndude audit). Stream URL nie inline w
    # SSR HTML (player JS init po Play click), więc WebView fallback: mobile pobiera
    # /watch/<id> z phone IP, player JS dekoduje video.src, INJECTED_JS scrape.
    # 0 VPS bandwidth — zgodne z pre-public bandwidth/anonimowość priorytet.
    "porndoecom": _vps_blocked_fallback.extract,
}


def try_extract(sitetag: str, page_url: str) -> list[StreamSource] | None:
    """Próbuje rozwiązać stream URL dla danego tube'a + page_url.

    Zwraca listę StreamSource (różne quality/kontener) lub None gdy:
      - brak extractora dla tego sitetag
      - extractor zwrócił None / nie znalazł URL'a

    Raises HosterDead gdy embed page wprost mówi że video deleted/not found —
    caller (playback.py) łapie i oznacza playback_source.dead_at.
    """
    extractor = _REGISTRY.get(sitetag)
    if extractor is None:
        return None
    try:
        return extractor(page_url)
    except (HosterDead, TubePageError):
        raise
    except Exception as e:
        log.warning("extractor for %s failed on %s: %s", sitetag, page_url, e)
        return None


def supported_sitetags() -> tuple[str, ...]:
    """Zwraca listę sitetag-ów które mają zarejestrowany extractor."""
    return tuple(_REGISTRY.keys())


__all__ = [
    "try_extract",
    "supported_sitetags",
    "StreamSource",
    "HosterDead",
    "TubePageError",
    "extract_stream_from_hoster",
    "unpack_packer",
    "fetch_tube_html",
    "browser_get",
]
