"""Check targets: selection, the Cloudflare ban, switching on outages and what that means for the statistics."""

import asyncio
import contextlib

import pytest

from proxyscraper import checker as ck
from proxyscraper import output, pipeline
from proxyscraper.checker import CheckResult
from proxyscraper.geo import GeoResolver
from proxyscraper.judges import Judge, JudgeProbe, JudgeWatch, behind_cloudflare, probe_judge, rank_judges
from proxyscraper.options import RunOptions
from proxyscraper.ui import CheckDashboard, LiveStats


@pytest.mark.parametrize("ip, expected", [
    ("104.16.184.241", True),    # icanhazip.com
    ("172.67.74.152", True),     # api.ipify.org
    ("34.251.184.218", False),   # checkip.amazonaws.com
    ("65.108.151.63", False),
])
def test_cloudflare_detection(ip, expected):
    assert behind_cloudflare(ip) is expected


async def ip_echo(reader, writer):
    head = await reader.readuntil(b"\r\n\r\n")
    body = b"203.0.113.7\n" if head.startswith(b"GET /ip ") else b"<html>nope</html>"
    writer.write(b"HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n" % len(body) + body)
    await writer.drain()
    writer.close()


def test_probe_accepts_only_a_bare_ip():
    async def go():
        srv = await asyncio.start_server(ip_echo, "127.0.0.1", 0)
        port = srv.sockets[0].getsockname()[1]
        try:
            return (await probe_judge(Judge("127.0.0.1", "/ip", port), timeout=2),
                    await probe_judge(Judge("127.0.0.1", "/", port), timeout=2),
                    await probe_judge(Judge("127.0.0.1", "/ip", 1), timeout=2))
        finally:
            srv.close()

    good, html, dead = asyncio.run(go())
    assert good is not None and good.ip == "127.0.0.1" and good.seen_ip == "203.0.113.7"
    assert html is None and dead is None


def test_rank_keeps_the_preference_order_and_drops_unreachable():
    judges = [Judge("a"), Judge("b"), Judge("c")]

    async def probe(judge, timeout):
        # "b" answers fastest, but "a" is the better target – the order stays
        return None if judge.host == "c" else JudgeProbe(judge, "1.1.1.1", {"a": 300, "b": 50}[judge.host])

    ranked = asyncio.run(rank_judges(judges, probe=probe))
    assert [r.judge.host for r in ranked] == ["a", "b"]


class FakeProbes:
    def __init__(self, down=()):
        self.down = set(down)

    async def __call__(self, judge, timeout=5.0, ip=None):
        return None if judge.host in self.down else JudgeProbe(judge, ip or "2.2.2.2", 100)


def make_watch(probes, hosts=("a", "b", "c")):
    ranked = [JudgeProbe(Judge(h), "1.1.1.1", 100) for h in hosts]
    used = []
    watch = JudgeWatch(ranked, used.append, probe=probes)
    return watch, used


def test_watch_needs_two_failures_before_switching():
    probes = FakeProbes(down={"a"})
    watch, used = make_watch(probes)
    asyncio.run(watch.check_once())
    assert used == [] and watch.current.judge.host == "a"
    asyncio.run(watch.check_once())
    assert [p.judge.host for p in used] == ["b"] and watch.switches == ["a → b"]


def test_watch_skips_reserves_that_are_down_too():
    watch, _used = make_watch(FakeProbes(down={"a", "b"}))
    for _ in range(2):
        asyncio.run(watch.check_once())
    assert watch.current.judge.host == "c"
    assert [r.judge.host for r in watch.reserve] == ["b", "a"]  # the one that went down goes to the end


def test_watch_recovers_after_a_single_hiccup():
    probes = FakeProbes(down={"a"})
    watch, used = make_watch(probes)
    asyncio.run(watch.check_once())
    probes.down.clear()
    asyncio.run(watch.check_once())
    asyncio.run(watch.check_once())
    assert used == [] and watch.failures == 0


class MovedBackend:
    """One hostname, the DNS now points to a new backend: the pinned old address is dead, the new one works."""

    def __init__(self, dead=("1.1.1.1",), fresh="2.2.2.2"):
        self.dead, self.fresh = set(dead), fresh
        self.calls = []

    async def __call__(self, judge, timeout=5.0, ip=None):
        self.calls.append((judge.host, ip))
        if ip is None:  # fresh DNS resolution
            return JudgeProbe(judge, self.fresh, 100)
        return None if ip in self.dead else JudgeProbe(judge, ip, 100)


def test_watch_probes_the_pinned_ip_and_follows_a_moved_backend():
    """A healthy new backend under the same hostname must not hide that the pinned path is dead."""
    probes = MovedBackend()
    c = ck.Checker("1.1.1.1", set(), timeout=1, connect_timeout=1, judge=Judge("a"))
    ranked = [JudgeProbe(Judge("a"), "1.1.1.1", 100), JudgeProbe(Judge("b"), "3.3.3.3", 100)]
    watch = JudgeWatch(ranked, lambda new: c.use_judge(new.judge, new.ip), probe=probes)
    switched, oks = [], []
    watch.on_switch = lambda old, new: switched.append((old.ip, new.ip))
    watch.on_ok = lambda: oks.append(1)
    asyncio.run(watch.check_once())
    assert oks == [] and watch.failures == 1 and c.judge_ip == "1.1.1.1"
    asyncio.run(watch.check_once())
    # same host, new address: watch and checker move together, and it counts as a switch (recheck logic)
    assert watch.current.judge.host == "a" and watch.current.ip == "2.2.2.2"
    assert c.judge_ip == "2.2.2.2" and c.judge_ip_bytes == bytes([2, 2, 2, 2])
    assert switched == [("1.1.1.1", "2.2.2.2")]
    assert watch.switches == ["a (1.1.1.1 → 2.2.2.2)"]
    assert [r.judge.host for r in watch.reserve] == ["b"]
    asyncio.run(watch.check_once())
    assert oks == [1] and probes.calls[-1] == ("a", "2.2.2.2")  # from now on the new address is watched


def test_watch_stays_on_a_working_pinned_ip_when_dns_rotates():
    probes = MovedBackend(dead=())  # DNS hands out another address each time, the pinned one keeps working
    watch, used = make_watch(probes)
    for _ in range(3):
        asyncio.run(watch.check_once())
    assert used == [] and watch.current.ip == "1.1.1.1" and watch.failures == 0
    assert all(ip == "1.1.1.1" for _, ip in probes.calls)


def test_watch_switches_to_a_reserve_when_the_host_is_down_everywhere():
    class Down(MovedBackend):
        async def __call__(self, judge, timeout=5.0, ip=None):
            if judge.host == "a":
                return None
            return await super().__call__(judge, timeout, ip)

    watch, used = make_watch(Down())
    for _ in range(2):
        asyncio.run(watch.check_once())
    assert [p.judge.host for p in used] == ["b"] and watch.switches == ["a → b"]


def test_probe_can_ask_exactly_the_pinned_ip(monkeypatch):
    async def no_dns(self, host, port, family=0, **kw):
        raise AssertionError("must not resolve again")

    async def go():
        srv = await asyncio.start_server(ip_echo, "127.0.0.1", 0)
        port = srv.sockets[0].getsockname()[1]
        monkeypatch.setattr(asyncio.BaseEventLoop, "getaddrinfo", no_dns)
        try:
            return await probe_judge(Judge("judge.example", "/ip", port), timeout=2, ip="127.0.0.1")
        finally:
            srv.close()

    pinned = asyncio.run(go())
    assert pinned is not None and pinned.ip == "127.0.0.1" and pinned.seen_ip == "203.0.113.7"


def test_checker_can_switch_judge_mid_run():
    c = ck.Checker("1.2.3.4", set(), timeout=1, connect_timeout=1)
    assert b"Host: checkip.amazonaws.com\r\n" in c.request
    c.use_judge(Judge("ifconfig.me", "/ip"), "34.160.111.145")
    assert c.request.startswith(b"GET /ip HTTP/1.1\r\nHost: ifconfig.me\r\n")
    assert c.http_proxy_request.startswith(b"GET http://ifconfig.me/ip HTTP/1.1\r\n")
    assert c.judge_ip_bytes == bytes([34, 160, 111, 145])


def test_outage_mid_run_rechecks_and_keeps_stats_clean(tmp_path):
    """The first three checks fall into an outage of the check target. After the switch exactly these
    three are repeated; for the statistics every proxy counts only once."""
    jobs = [f"http 1.1.1.{i}:80" for i in range(1, 7)]

    class StubWatch:
        on_ok = on_switch = None

        async def run(self):
            self.on_ok()  # the first probe before the checks was good
            await asyncio.sleep(3600)

    watch = StubWatch()

    class OutageChecker:
        def __init__(self):
            self.calls = 0
            self.down = True

        async def check(self, key):
            self.calls += 1
            if self.down and self.calls > 3:  # the watchdog notices the outage and switches
                self.down = False
                watch.on_switch(JudgeProbe(Judge("a"), "1.1.1.1", 1), JudgeProbe(Judge("b"), "2.2.2.2", 1))
            if self.down:
                return None
            ptype, proxy = key.split(" ")
            return CheckResult(key, ptype, proxy, 100, "9.9.9.9")

        async def confirm(self, r):
            return True

        async def tampers(self, r):
            return False

        async def enrich(self, r):
            r.https = True

    async def go():
        stats = LiveStats({"http": len(jobs)})
        writer = output.ResultWriter(run_dir=tmp_path / "run")
        dashboard = CheckDashboard(stats, writer.live_path, 1, True)
        opts = RunOptions(no_geo=True, concurrency=1)
        run = await pipeline.run_checks(jobs, OutageChecker(), opts, dashboard, writer, GeoResolver(enabled=False),
                                        live_factory=lambda _: contextlib.nullcontext(), watch=watch)
        return run, stats, dashboard

    run, stats, dashboard = asyncio.run(go())
    assert run.working == set(jobs)            # all six work after the switch
    assert sorted(run.checked) == sorted(jobs)  # each exactly once – no false failures
    assert run.rechecked == 3 and run.judge_switches == ["a → b"]
    assert stats.total == 9 and stats.total_by_type["http"] == 9
    assert "b" in dashboard.judge_note


def test_check_that_fails_after_the_switch_is_rechecked_not_counted(tmp_path):
    """Found by Copilot: a check still runs with the old target while switching, and only fails
    afterwards. It must not count as a real failure in the statistics – it gets repeated."""
    jobs = ["http 1.1.1.1:80", "http 2.2.2.2:80"]
    switched = None  # create asyncio.Event only inside the loop (Python 3.9)

    class StubWatch:
        on_ok = on_switch = None

        async def run(self):
            self.on_ok()
            await asyncio.sleep(3600)

    watch = StubWatch()

    class SlowChecker:
        def __init__(self):
            self.seen = []

        async def check(self, key):
            self.seen.append(key)
            if key == "http 1.1.1.1:80" and not switched.is_set():
                await asyncio.sleep(0.05)      # still on its way with the old target ...
                await switched.wait()
                return None                    # ... and only fails after the switch
            if key == "http 2.2.2.2:80" and not switched.is_set():
                watch.on_switch(JudgeProbe(Judge("a"), "1.1.1.1", 1), JudgeProbe(Judge("b"), "2.2.2.2", 1))
                switched.set()
            ptype, proxy = key.split(" ")
            return CheckResult(key, ptype, proxy, 100, "9.9.9.9")

        async def confirm(self, r):
            return True

        async def tampers(self, r):
            return False

        async def enrich(self, r):
            r.https = True

    checker = SlowChecker()

    async def go():
        nonlocal switched
        switched = asyncio.Event()
        stats = LiveStats({"http": len(jobs)})
        writer = output.ResultWriter(run_dir=tmp_path / "run")
        dashboard = CheckDashboard(stats, writer.live_path, 2, True)
        return await pipeline.run_checks(jobs, checker, RunOptions(no_geo=True, concurrency=2), dashboard, writer,
                                         GeoResolver(enabled=False), live_factory=lambda _: contextlib.nullcontext(),
                                         watch=watch)

    run = asyncio.run(go())
    assert checker.seen.count("http 1.1.1.1:80") == 2   # checked again after the switch
    assert run.working == set(jobs) and sorted(run.checked) == sorted(jobs)
    assert run.rechecked == 1
    assert len(run.results) == 2                        # no duplicate hit


def test_failures_before_the_last_good_probe_stay_counted(tmp_path):
    """Probe ok -> a few real failures -> ok again -> outage: only what failed after the second
    good probe is repeated."""
    jobs = [f"http 1.1.1.{i}:80" for i in range(1, 7)]

    class StubWatch:
        on_ok = on_switch = None

        async def run(self):
            await asyncio.sleep(3600)

    watch = StubWatch()

    class Checker:
        calls = 0

        async def check(self, key):
            self.calls += 1
            if self.calls == 3:
                await asyncio.sleep(0.01)
                watch.on_ok()       # check target still reachable after two real failures
                await asyncio.sleep(0.01)
            if self.calls == 5:
                watch.on_switch(JudgeProbe(Judge("a"), "1.1.1.1", 1), JudgeProbe(Judge("b"), "2.2.2.2", 1))
            return None             # everything fails – but the first two before the good probe

        async def confirm(self, r):
            return True

        async def tampers(self, r):
            return False

    async def go():
        stats = LiveStats({"http": len(jobs)})
        writer = output.ResultWriter(run_dir=tmp_path / "run")
        dashboard = CheckDashboard(stats, writer.live_path, 1, True)
        return await pipeline.run_checks(jobs, Checker(), RunOptions(no_geo=True, concurrency=1), dashboard, writer,
                                         GeoResolver(enabled=False), live_factory=lambda _: contextlib.nullcontext(),
                                         watch=watch)

    run = asyncio.run(go())
    assert "http 1.1.1.1:80" in run.checked and "http 1.1.1.2:80" in run.checked  # real failures
    assert run.rechecked == 2  # 3 and 4 fell into the outage


def test_second_switch_only_rechecks_failures_under_the_new_target(tmp_path):
    """After a switch the successful probe of the new target counts as a good probe: if that one goes
    down too, only the failures since the first switch are repeated – not the ones before."""
    jobs = [f"http 1.1.1.{i}:80" for i in range(1, 7)]

    class StubWatch:
        on_ok = on_switch = None

        async def run(self):
            await asyncio.sleep(3600)

    watch = StubWatch()
    a, b, c = (JudgeProbe(Judge(h), "1.1.1.1", 1) for h in "abc")

    class Checker:
        calls = 0

        async def check(self, key):
            self.calls += 1
            await asyncio.sleep(0.01)
            if self.calls == 3:
                watch.on_switch(a, b)   # 1 and 2 fell into the outage of a
            await asyncio.sleep(0.01)
            if self.calls == 5:
                watch.on_switch(b, c)   # 4 fell into the outage of b (3 was still running with a)
            return None

        async def confirm(self, r):
            return True

        async def tampers(self, r):
            return False

    async def go():
        stats = LiveStats({"http": len(jobs)})
        writer = output.ResultWriter(run_dir=tmp_path / "run")
        dashboard = CheckDashboard(stats, writer.live_path, 1, True)
        return await pipeline.run_checks(jobs, Checker(), RunOptions(no_geo=True, concurrency=1), dashboard, writer,
                                         GeoResolver(enabled=False), live_factory=lambda _: contextlib.nullcontext(),
                                         watch=watch)

    run = asyncio.run(go())
    assert run.judge_switches == ["a → b", "b → c"]
    # 1, 2 (outage a) + 3 (still running with a, ended after the switch) + 4, 5 (outage b); after that the
    # baseline is new – the repeats under c count normally
    assert sorted(set(run.checked)) == sorted(jobs) and len(run.checked) == len(jobs)


def test_a_single_cloudflare_address_rules_out_the_target(monkeypatch):
    async def fake_getaddrinfo(self, host, port, family=0, **kw):
        return [(2, 1, 6, "", ("34.251.184.218", port)), (2, 1, 6, "", ("104.16.184.241", port))]

    monkeypatch.setattr(asyncio.BaseEventLoop, "getaddrinfo", fake_getaddrinfo)
    assert asyncio.run(probe_judge(Judge("gemischt.example"), timeout=1)) is None


def test_own_ip_falls_back_to_what_the_judges_saw(monkeypatch):
    from proxyscraper import app
    from proxyscraper.judges import Judge as J

    async def no_own_ips():
        return []

    async def ranked():
        return [JudgeProbe(J("a"), "1.1.1.1", 10, "87.150.19.11"), JudgeProbe(J("b"), "2.2.2.2", 10, "87.150.19.11"),
                JudgeProbe(J("c"), "3.3.3.3", 10, "2003:d6::1")]  # IPv6 doesn't help the checker

    async def no_confirm():
        return None

    monkeypatch.setattr(app, "get_own_ips", no_own_ips)
    monkeypatch.setattr(app, "rank_judges", ranked)
    monkeypatch.setattr(app, "confirm_target", no_confirm)
    run = app.Run(RunOptions(no_geo=True), show_banner=False)
    assert asyncio.run(run.prepare_network()) and run.own_ips == ["87.150.19.11"]


def test_verify_control_asks_the_pinned_judge_address(monkeypatch):
    """--pick --verify / --diagnose have no JudgeWatch: their direct control must check the pinned address too."""
    from proxyscraper import app, verify
    from proxyscraper.checker import BASIC

    asked = []

    async def ranked():
        return [JudgeProbe(Judge("a"), "1.1.1.1", 100, "5.5.5.5")]

    async def own_ips():
        return ["5.5.5.5"]

    async def nothing(*args, **kwargs):
        return None

    async def fake_probe(judge, timeout=5.0, ip=None):
        asked.append(ip)
        return None

    monkeypatch.setattr(verify, "rank_judges", ranked)
    monkeypatch.setattr(app, "get_own_ips", own_ips)
    monkeypatch.setattr(app, "confirm_target", nothing)
    monkeypatch.setattr(verify, "probe_judge", fake_probe)

    async def go():
        checker = await verify.prepare(1, 1)
        await checker.controls[BASIC]()

    asyncio.run(go())
    assert asked == ["1.1.1.1"]


def test_switch_during_the_second_chances_still_rechecks(tmp_path, monkeypatch):
    """The second chances run after the workers: a check target switch then must still repeat the failures of
    the outage – there is no worker left to take them from the queue unless run_checks starts one."""
    from proxyscraper.checkstate import CONFIRM, CONTENT, Check

    monkeypatch.setattr(pipeline, "SECOND_CHANCE_DELAY", 0.0)
    victim, flaky = "http 1.1.1.1:80", "http 2.2.2.2:80"

    class StubWatch:
        on_ok = on_switch = None

        async def run(self):
            self.on_ok()
            await asyncio.sleep(3600)

    watch = StubWatch()

    class Checker:
        def __init__(self):
            self.down = True
            self.confirms = 0

        async def check(self, key):
            if key == victim and self.down:
                return None  # falls into the outage
            ptype, proxy = key.split(" ")
            return CheckResult(key, ptype, proxy, 100, "9.9.9.9")

        async def confirm(self, r):
            self.confirms += 1
            if self.confirms == 1:
                r.checks[CONFIRM] = Check("failed", "connect_timeout")
                return False
            if self.down:  # during the second chance the watchdog notices the outage
                self.down = False
                watch.on_switch(JudgeProbe(Judge("a"), "1.1.1.1", 1), JudgeProbe(Judge("b"), "2.2.2.2", 1))
            r.checks[CONFIRM] = Check("passed", "")
            return True

        async def tampers(self, r):
            r.checks[CONTENT] = Check("passed", "")
            return False

        async def enrich(self, r):
            r.https = True

    async def go():
        jobs = [victim, flaky]
        stats = LiveStats({"http": 2})
        writer = output.ResultWriter(run_dir=tmp_path / "run")
        dashboard = CheckDashboard(stats, writer.live_path, 1, True)
        run = await pipeline.run_checks(jobs, Checker(), RunOptions(no_geo=True, concurrency=1), dashboard, writer,
                                        GeoResolver(enabled=False), live_factory=lambda _: contextlib.nullcontext(),
                                        watch=watch)
        return run

    run = asyncio.run(go())
    assert run.working == {victim, flaky}
    assert sorted(run.checked) == [victim, flaky] and run.rechecked == 1
