"""Minimal HTTP client on plain asyncio + ssl – for sources, APIs and check targets."""

from __future__ import annotations

import asyncio
import ssl
from functools import lru_cache
from typing import Dict, List, Optional, Set, Tuple
from urllib.parse import urljoin, urlsplit

from .httpframing import ChunkedDecoder
from .netpolicy import PUBLIC_ONLY, TargetPolicy

USER_AGENT = (
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
    "(KHTML, like Gecko) Chrome/128.0 Safari/537.36"
)
MAX_BODY = 64 * 1024 * 1024

# hosts whose certificate couldn't be verified (typically: a firewall with TLS inspection like Sophos)
INSECURE_HOSTS: Set[str] = set()


@lru_cache(maxsize=None)
def ssl_context() -> ssl.SSLContext:
    # build once instead of per connection – otherwise loading the CA file blocks the event loop every time
    try:
        import certifi  # python.org Python on macOS often has no system certificates

        return ssl.create_default_context(cafile=certifi.where())
    except ImportError:
        return ssl.create_default_context()


@lru_cache(maxsize=None)
def _insecure_ssl_context() -> ssl.SSLContext:
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    return ctx


CONDITIONAL_HEADERS = {"If-None-Match", "If-Modified-Since"}


async def http_request(
    url: str,
    timeout: float = 15.0,
    headers: Optional[Dict[str, str]] = None,
    method: str = "GET",
    body: Optional[bytes] = None,
    max_redirects: int = 3,
    insecure_fallback: bool = True,
    policy: Optional[TargetPolicy] = None,
) -> Tuple[int, Dict[bytes, bytes], bytes]:
    """One HTTP/1.1 request -> (status, headers, body). Follows redirects for GET.

    If certificate verification fails, it is only retried unverified for GETs without a body and without
    custom headers – so for public proxy lists, never with API tokens or sent data. The only headers
    allowed are If-None-Match / If-Modified-Since (ETag cache), which carry no secret.
    The lists are public, and every proxy from them gets checked on its own anyway.

    policy: for URLs nobody chose by hand (see netpolicy) – the URL and every redirect hop are checked, and
    the connection goes to an address the check approved. None: unrestricted (curated/own sources, APIs).
    """
    headers = dict(headers or {})
    for _ in range(max_redirects + 1):
        extra = "".join(f"{k}: {v}\r\n" for k, v in headers.items())
        if body is not None:
            extra += f"Content-Length: {len(body)}\r\n"
        target = await policy.resolve(url) if policy is not None else None
        u = urlsplit(url)
        https = u.scheme == "https"
        port = u.port or (443 if https else 80)
        path = (u.path or "/") + (f"?{u.query}" if u.query else "")
        # unverified only for GETs without a body and without custom headers (except the conditional ETag headers,
        # they carry no secret). Never tokens or sent data over an unverified connection.
        insecure_ok = insecure_fallback and method == "GET" and body is None and \
            set(headers) <= CONDITIONAL_HEADERS
        pinned = {"addresses": target.addresses} if target is not None else {}
        reader, writer = await _connect(u.hostname, port, https, allow_insecure=insecure_ok, timeout=timeout, **pinned)
        try:
            writer.write(
                f"{method} {path} HTTP/1.1\r\nHost: {host_header(u.hostname, port, https)}\r\n"
                f"User-Agent: {USER_AGENT}\r\n"
                f"Accept: */*\r\nAccept-Encoding: identity\r\n{extra}Connection: close\r\n\r\n".encode()
                + (body or b"")
            )
            await writer.drain()
            status, resp_headers, resp_body = await asyncio.wait_for(read_response(reader), timeout)
        finally:
            writer.close()

        if method == "GET" and status in (301, 302, 303, 307, 308) and b"location" in resp_headers:
            url = urljoin(url, resp_headers[b"location"].decode())
            headers = without_credentials(headers, u, urlsplit(url))
            continue
        return status, resp_headers, resp_body
    raise ConnectionError("too many redirects")


CREDENTIAL_HEADERS = {"authorization", "cookie", "proxy-authorization"}


def without_credentials(headers: Dict[str, str], before, after) -> Dict[str, str]:
    """Headers for the next redirect hop: a token (GitHub's Authorization: Bearer) is meant for the host it was
    given to – a redirect to another host, or from https down to http, doesn't get it."""
    same_host = (before.hostname or "").lower() == (after.hostname or "").lower()
    downgrade = before.scheme == "https" and after.scheme != "https"
    if same_host and not downgrade:
        return headers
    return {k: v for k, v in headers.items() if k.lower() not in CREDENTIAL_HEADERS}


def host_header(hostname: str, port: Optional[int], https: bool) -> str:
    """Value of the Host header: the port only when it isn't the scheme's default, IPv6 literals in brackets."""
    host = f"[{hostname}]" if ":" in hostname else hostname
    return host if port is None or port == (443 if https else 80) else f"{host}:{port}"


def authority(hostname: str, port: int) -> str:
    """host:port for a CONNECT line – always with the port, IPv6 literals in brackets."""
    return f"[{hostname}]:{port}" if ":" in hostname else f"{hostname}:{port}"


async def http_get(
    url: str, timeout: float = 15.0, max_redirects: int = 3, headers: Optional[Dict[str, str]] = None,
    policy: Optional[TargetPolicy] = None, insecure_fallback: bool = True,
) -> bytes:
    """GET that raises an exception for anything but 200."""
    status, _, body = await http_request(url, timeout, headers, max_redirects=max_redirects, policy=policy,
                                         insecure_fallback=insecure_fallback)
    if status != 200:
        raise ConnectionError(f"HTTP {status}")
    return body


async def verified_get(
    url: str, timeout: float = 15.0, max_redirects: int = 3, headers: Optional[Dict[str, str]] = None
) -> bytes:
    """http_get without the unverified retry – for data that is used as it arrives, not checked again: the live
    list (handed out by --pick, the Python API and the MCP tools) and the country/provider databases. Public
    proxy lists keep the fallback (behind a TLS-inspecting firewall they'd fail otherwise), because every
    proxy from them is checked on its own before anyone gets it."""
    return await http_get(url, timeout, max_redirects, headers, insecure_fallback=False)


async def public_get(
    url: str, timeout: float = 15.0, max_redirects: int = 3, headers: Optional[Dict[str, str]] = None
) -> bytes:
    """http_get restricted to public addresses – for URLs found automatically in files others wrote."""
    return await http_get(url, timeout, max_redirects, headers, policy=PUBLIC_ONLY)


async def _connect(host: str, port: int, https: bool, allow_insecure: bool, timeout: float,
                   addresses: Optional[List[str]] = None):
    """Connects to host – or, if given, to one of the already checked addresses of host (tried in order),
    while TLS still verifies the certificate for host and sends it as SNI."""
    if addresses is None:
        return await _open(host, host, port, https, allow_insecure, timeout)
    deadline = asyncio.get_running_loop().time() + timeout
    error: Optional[BaseException] = None
    for address in addresses:
        left = deadline - asyncio.get_running_loop().time()
        if left <= 0:
            break
        try:
            return await _open(address, host, port, https, allow_insecure, left)
        except ssl.SSLError:
            raise  # the server answered: another address of the same name won't have another certificate
        except (OSError, asyncio.TimeoutError) as e:
            error = e
    raise error or asyncio.TimeoutError()


async def _open(dial: str, host: str, port: int, https: bool, allow_insecure: bool, timeout: float):
    if not https:
        return await asyncio.wait_for(asyncio.open_connection(dial, port), timeout)
    ctx = _insecure_ssl_context() if allow_insecure and host in INSECURE_HOSTS else ssl_context()
    try:
        return await asyncio.wait_for(asyncio.open_connection(dial, port, ssl=ctx, server_hostname=host), timeout)
    except ssl.SSLCertVerificationError:
        if not allow_insecure:
            raise
        INSECURE_HOSTS.add(host)
        return await asyncio.wait_for(
            asyncio.open_connection(dial, port, ssl=_insecure_ssl_context(), server_hostname=host), timeout
        )


async def read_response(reader) -> Tuple[int, Dict[bytes, bytes], bytes]:
    head = await reader.readuntil(b"\r\n\r\n")
    lines = head[:-4].split(b"\r\n")
    parts = lines[0].split()
    status = int(parts[1]) if len(parts) > 1 and parts[1].isdigit() else 0
    headers: Dict[bytes, bytes] = {}
    for line in lines[1:]:
        k, _, v = line.partition(b":")
        headers[k.strip().lower()] = v.strip()

    # respect Content-Length: servers with keep-alive (e.g. checkip.amazonaws.com) don't close
    # the connection otherwise, and reading to the end would run into the timeout.
    if headers.get(b"transfer-encoding", b"").lower() == b"chunked":
        body = await read_chunked(reader)
    elif headers.get(b"content-length", b"").isdigit():
        length = int(headers[b"content-length"])
        if length > MAX_BODY:
            raise ConnectionError("response too large")
        body = await reader.readexactly(length)
    else:
        body = await read_all(reader)
    return status, headers, body


async def read_all(reader, limit: int = MAX_BODY) -> bytes:
    # some servers (e.g. Cloudflare) end with RST instead of FIN – keep the data already read
    buf = bytearray()
    while len(buf) < limit:
        try:
            chunk = await reader.read(65536)
        except ConnectionResetError:
            break
        if not chunk:
            break
        buf += chunk
    return bytes(buf)


async def read_chunked(reader, limit: Optional[int] = None) -> bytes:
    """A chunked body, read only up to its end – not to EOF, which a keep-alive server never sends."""
    decoder = ChunkedDecoder(MAX_BODY if limit is None else limit)
    while True:
        try:
            chunk = await reader.read(65536)
        except ConnectionResetError:
            chunk = b""
        if not chunk:
            return decoder.finish()  # FramingError if cut off
        if decoder.feed(chunk):
            return bytes(decoder.body)


def dechunk(data: bytes) -> bytes:
    """Lenient decoding of a complete, already read chunked body (kept for callers outside the package);
    readers use ChunkedDecoder, which also knows where the body ends."""
    out, i = bytearray(), 0
    while i < len(data):
        j = data.find(b"\r\n", i)
        if j < 0:
            break
        try:
            size = int(data[i:j].split(b";")[0], 16)
        except ValueError:
            break
        if size == 0:
            break
        out += data[j + 2 : j + 2 + size]
        i = j + 2 + size + 2
    return bytes(out)
