"""Unit tests for link_extract — pure, no DB/network."""

from backend.app.services.link_extract import extract_external_links, host_for


def test_host_classification_covers_all_and_rejects_others():
    assert host_for("https://mega.nz/file/x#k") == "mega"
    assert host_for("https://www.dropbox.com/s/x?dl=0") == "dropbox"
    assert host_for("https://drive.google.com/file/d/ID/view") == "gdrive"
    assert host_for("https://pixeldrain.com/u/abc") == "pixeldrain"
    assert host_for("https://www.mediafire.com/file/x") == "mediafire"
    assert host_for("https://example.com/x") is None


def test_anchor_preserves_fragment_and_label():
    html = ('<p>Watch: '
            '<a href="https://mega.nz/file/Bux0FRrZ#b2Pn9841">Mega - Streamable</a></p>')
    links = extract_external_links(html)
    assert len(links) == 1
    assert links[0].host == "mega"
    # The #fragment (mega's decryption key) MUST survive.
    assert links[0].url == "https://mega.nz/file/Bux0FRrZ#b2Pn9841"
    assert links[0].label == "Mega - Streamable"


def test_extracts_all_five_hosts():
    html = (
        '<a href="https://mega.nz/file/a#k">m</a>'
        '<a href="https://drive.google.com/file/d/ID/view?usp=sharing">g</a>'
        '<a href="https://www.mediafire.com/file/x/y">mf</a>'
        '<a href="https://www.dropbox.com/s/x/y?dl=0">db</a>'
        '<a href="https://pixeldrain.com/u/ems4UomN">px</a>'
    )
    hosts = {lnk.host for lnk in extract_external_links(html)}
    assert hosts == {"mega", "gdrive", "mediafire", "dropbox", "pixeldrain"}


def test_bare_url_in_text():
    html = "Full film: https://pixeldrain.com/u/ems4UomN and more prose."
    links = extract_external_links(html)
    assert len(links) == 1
    assert links[0].host == "pixeldrain"
    assert links[0].url == "https://pixeldrain.com/u/ems4UomN"  # trailing '.' trimmed
    assert links[0].label is None


def test_unwraps_patreon_redirect():
    html = ('<a href="https://www.patreon.com/redirect?'
            'url=https%3A%2F%2Fmega.nz%2Ffile%2Fz%23key">x</a>')
    links = extract_external_links(html)
    assert len(links) == 1
    assert links[0].host == "mega"
    assert links[0].url == "https://mega.nz/file/z#key"


def test_ignores_unsupported_and_dedups_anchor_label_wins():
    html = (
        '<a href="https://example.com/x">no</a>'
        '<a href="https://mega.nz/file/dup#k">a</a>'
        ' bare https://mega.nz/file/dup#k'
    )
    links = extract_external_links(html)
    assert len(links) == 1            # example.com dropped; mega dup collapsed
    assert links[0].label == "a"      # anchor (with label) wins over bare


def test_amp_escaped_query_is_unescaped():
    html = '<a href="https://drive.google.com/uc?id=ID&amp;export=download">g</a>'
    links = extract_external_links(html)
    assert links[0].url == "https://drive.google.com/uc?id=ID&export=download"


def test_empty_inputs():
    assert extract_external_links(None) == []
    assert extract_external_links("") == []
    assert extract_external_links("<p>no links here</p>") == []
