"""Per-CDN cross-IP test: VPS resolve URL → check if works z różnego IP (laptop).

Identifies time-bound (safe for mobile direct) vs IP-bound (requires VPS proxy).

Usage: set GOON_VPS_SSH=root@<your-vps> in env before running.
"""
import os
import subprocess

from app.extractors import try_extract

VPS_SSH = os.environ.get("GOON_VPS_SSH", "root@your-vps.example.com")

TESTS = [
    ("xvideoscom", "https://www.xvideos.com/video.ulklkike487/i_helped_my_stepbro_to_cum_with_my_hands_boobs_and_mouth_-_julia_goddess"),
    ("xnxxcom", "https://www.xnxx.com/video-5pc3of3/kimberly_kendall_and_jon_jon"),
    ("pornhubcom", "https://pornhub.com/view_video.php?viewkey=68597f719a5db"),
    ("youporncom", "https://www.youporn.com/watch/17136141/matchmaker-bangs-milf-teen-she-matched"),
    ("redtubecom", "https://redtube.com/43048211"),
    ("porntrexcom", "https://www.porntrex.com/video/1626524/sidra-sage-viral-video"),
    ("epornercom", "https://www.eporner.com/video-HPQBR5TeKjv/annie-reiss-5k/"),
    ("hqpornercom", "https://hqporner.com/hdporn/110000-just_for_test.html"),
    ("freshpornoorg", "https://freshporno.org/videos/a-caring-step-sister/"),
    ("sxyprncom", "https://sxyprn.com/post/68adbb3b6bf1b.html"),
]

for sitetag, page in TESTS:
    try:
        ss = try_extract(sitetag, page)
    except Exception as e:
        print(f"{sitetag}: extract failed: {e}")
        continue
    if not ss:
        print(f"{sitetag}: no sources")
        continue
    # Take first non-hoster source (the one that goes through proxy currently)
    first = next((s for s in ss if s.type in ("mp4", "m3u8", "hls")), None)
    if not first:
        print(f"{sitetag}: no mp4/m3u8 source (only hoster)")
        continue
    url = first.link
    # VPS HEAD
    vps_res = subprocess.run(
        ["ssh", VPS_SSH, f"curl -s -A 'Mozilla/5.0' --max-time 8 -o /dev/null -w '%{{http_code}}' '{url}'"],
        capture_output=True, text=True, timeout=20,
    )
    print(f"{sitetag} type={first.type} domain={url.split('/')[2]} vps={vps_res.stdout.strip()}", end=" ", flush=True)
    print(f"  url-tail={url[-80:]}")
