"""QuantumProxies Web Unlocker client for Python 3.9 or later.

One file, standard library only: download it, put it next to your code, import it.

    from unlocker import Unlocker, UnlockerError

    client = Unlocker(api_key="qp_live_YOUR_API_KEY", retries=2)
    page = client.unlock("https://example.com/", country="us", format="markdown")
    print(page["status"], page["tier_served"], page["cost"]["bytes"])
    print(page["body"][:500])

Options are the fields of POST /api/v1/scraper/unlock in snake_case (country,
mode, format, max_bytes, timeout_ms, fail_on_block, session_id, actions,
cache_ttl and the others). They are sent as they are, as keyword arguments or
as one dict. Reference: https://quantumproxies.io/docs/

A page that is still blocked is not an error: the answer has "blocked": True.
Pass fail_on_block=True to get a 424 target_blocked error instead.

Errors are UnlockerError, with code, retryable, billed, hint, status and
request_id. With retries (0 to 2, default 0) a call whose error is retryable is
sent again after the pause the API asks for in Retry-After. Two cases are never
repeated, because the first call may have run: a connection that fails without
an answer, and an unlock whose method is not GET, HEAD or OPTIONS.
"""

import http.client
import json
import time
import urllib.error
import urllib.parse
import urllib.request
from email.utils import parsedate_to_datetime
from typing import Any, Dict, List, Optional, Union

__all__ = ["Unlocker", "UnlockerError"]

DEFAULT_BASE_URL = "https://app.quantumproxies.io"
USER_AGENT = "unlocker-client/1.0 (python)"
MAX_RETRIES = 2
DEFAULT_RETRY_WAIT = 2.0
MAX_RETRY_WAIT = 60.0
# The longest unlock call is 120 s (timeout_ms); the client waits a little longer.
DEFAULT_TIMEOUT = 150.0
REPEATABLE_METHODS = {"GET", "HEAD", "OPTIONS"}
# Answers that do not come from the unlocker's error catalogue (a gateway, the
# API key check) carry no "retryable" flag: these statuses count as retryable.
RETRYABLE_STATUS = {429, 502, 503, 504}
CODE_BY_STATUS = {401: "unauthorized", 429: "rate_limited"}


class UnlockerError(Exception):
    """An error answer of the Web Unlocker, or no answer at all (status 0)."""

    def __init__(
        self,
        message: str,
        status: int = 0,
        payload: Optional[Dict[str, Any]] = None,
        retry_after: Optional[float] = None,
        request_id: Optional[str] = None,
    ) -> None:
        super().__init__(message)
        payload = payload if isinstance(payload, dict) else {}
        self.message = message
        #: HTTP status of the answer; 0 when there was no answer.
        self.status = status
        code = payload.get("code")
        #: Code of the error catalogue (target_blocked, no_credit, rate_limited...); network_error when there was no answer.
        self.code = code if isinstance(code, str) else ("network_error" if status == 0 else CODE_BY_STATUS.get(status, "http_%d" % status))
        retryable = payload.get("retryable")
        #: Whether the same request may succeed later.
        self.retryable = retryable if isinstance(retryable, bool) else status in RETRYABLE_STATUS
        #: Whether the bytes moved were debited from the prepaid GB.
        self.billed = payload.get("billed") is True
        hint = payload.get("hint")
        #: What to do next, in one sentence.
        self.hint = hint if isinstance(hint, str) else ""
        rid = payload.get("request_id")
        #: Id of the exchange, to quote to support; None when the engine was not reached.
        self.request_id = rid if isinstance(rid, str) else request_id
        doc_url = payload.get("doc_url")
        self.doc_url = doc_url if isinstance(doc_url, str) else None
        #: Seconds the API asked to wait before a retry, or None.
        self.retry_after = retry_after
        #: The whole error payload (block details, usage, cost, buy_url...).
        self.payload = payload


class Unlocker:
    """Client of the REST call and of the batch endpoints.

    api_key   your API key
    base_url  default "https://app.quantumproxies.io"
    retries   0 to 2 (default 0): repeat a call whose error is retryable
    timeout   seconds the client waits for one answer (default 150)
    """

    def __init__(self, api_key: str, base_url: str = DEFAULT_BASE_URL, retries: int = 0, timeout: float = DEFAULT_TIMEOUT) -> None:
        if not isinstance(api_key, str) or not api_key:
            raise ValueError("api_key is required")
        self.api_key = api_key
        self.base_url = str(base_url).rstrip("/")
        self.retries = max(0, min(MAX_RETRIES, retries if isinstance(retries, int) else 0))
        self.timeout = timeout

    def unlock(self, url: str, options: Optional[Dict[str, Any]] = None, **fields: Any) -> Dict[str, Any]:
        """Unlock one URL. Returns the payload of the answer: status, body, blocked,
        tier_served, attempt_log, cache, usage, cost and the rest.

        The REST fields go in snake_case, as keyword arguments or as one dict.
        """
        if not isinstance(url, str) or not url:
            raise ValueError("url is required")
        body = dict(options or {})
        body.update(fields)
        body["url"] = url
        method = str(body.get("method") or "GET").upper()
        return self._call("POST", "/api/v1/scraper/unlock", body=body, repeatable=method in REPEATABLE_METHODS)

    def batch(self, requests: List[Union[str, Dict[str, Any]]], options: Optional[Dict[str, Any]] = None, **fields: Any) -> Dict[str, Any]:
        """Start a batch of 1 to 100 unlock calls: URLs, or dicts with "url" and any REST field.

        Options: defaults, webhook_url, concurrency, tier.
        Returns {"batch_id", "status", "total", "status_url"}.
        """
        if not isinstance(requests, (list, tuple)) or not requests:
            raise ValueError("requests must be a non-empty list of URLs or request dicts")
        body = dict(options or {})
        body.update(fields)
        body["requests"] = [{"url": r} if isinstance(r, str) else r for r in requests]
        return self._call("POST", "/api/v1/scraper/unlock/batch", body=body)

    def batch_status(self, batch_id: str, include_body: bool = False, offset: Optional[int] = None, limit: Optional[int] = None) -> Dict[str, Any]:
        """Progress and results of a batch."""
        query: Dict[str, Any] = {}
        if include_body:
            query["include"] = "body"
        if offset is not None:
            query["offset"] = offset
        if limit is not None:
            query["limit"] = limit
        return self._call("GET", "/api/v1/scraper/unlock/batch/" + urllib.parse.quote(str(batch_id), safe=""), query=query)

    def batch_cancel(self, batch_id: str) -> Dict[str, Any]:
        """Cancel the items of a batch that have not started."""
        return self._call("DELETE", "/api/v1/scraper/unlock/batch/" + urllib.parse.quote(str(batch_id), safe=""))

    # The same names as the Node.js client.
    batchStatus = batch_status
    batchCancel = batch_cancel

    def _call(self, method: str, path: str, body: Optional[Dict[str, Any]] = None, query: Optional[Dict[str, Any]] = None, repeatable: bool = True) -> Any:
        url = self.base_url + path
        if query:
            url += "?" + urllib.parse.urlencode(query)
        attempt = 0
        while True:
            try:
                return self._send(method, url, body)
            except UnlockerError as error:
                again = error.status != 0 and error.retryable and repeatable and attempt < self.retries
                if not again:
                    raise
                seconds = error.retry_after
                if seconds is None and isinstance(error.payload.get("retry_after"), (int, float)):
                    seconds = float(error.payload["retry_after"])
                time.sleep(max(0.0, min(MAX_RETRY_WAIT, DEFAULT_RETRY_WAIT if seconds is None else seconds)))
                attempt += 1

    def _send(self, method: str, url: str, body: Optional[Dict[str, Any]]) -> Any:
        headers = {"Authorization": "Bearer " + self.api_key, "Accept": "application/json", "User-Agent": USER_AGENT}
        data = None
        if body is not None:
            data = json.dumps(body).encode("utf-8")
            headers["Content-Type"] = "application/json"
        request = urllib.request.Request(url, data=data, headers=headers, method=method)
        try:
            with urllib.request.urlopen(request, timeout=self.timeout) as response:
                status, response_headers, raw = response.status, response.headers, response.read()
        except urllib.error.HTTPError as error:
            status, response_headers, raw = error.code, error.headers, error.read()
        except (urllib.error.URLError, http.client.HTTPException, OSError) as error:
            raise UnlockerError("No answer from %s: %s" % (urllib.parse.urlsplit(url).netloc, error)) from error
        try:
            parsed = json.loads(raw.decode("utf-8")) if raw else None
        except ValueError:
            # Not JSON: an answer that is not from the API (a gateway page, for instance).
            parsed = None
        is_error = isinstance(parsed, dict) and parsed.get("type") == "error"
        if 200 <= status < 300 and not is_error:
            # Every endpoint answers {type, message, payload}; the payload is what the caller wants.
            if isinstance(parsed, dict) and parsed.get("type") == "response" and "payload" in parsed:
                return parsed["payload"]
            return parsed
        payload = parsed.get("payload") if isinstance(parsed, dict) and isinstance(parsed.get("payload"), dict) else {}
        message = parsed.get("message") if isinstance(parsed, dict) and isinstance(parsed.get("message"), str) and parsed.get("message") else "HTTP %d" % status
        raise UnlockerError(
            message,
            status=status,
            payload=payload,
            retry_after=_retry_after_seconds(response_headers.get("Retry-After")),
            request_id=response_headers.get("x-qp-request-id"),
        )


def _retry_after_seconds(value: Optional[str]) -> Optional[float]:
    """Retry-After as seconds: a number of seconds, or an HTTP date."""
    if value is None or value == "":
        return None
    try:
        return max(0.0, float(value))
    except ValueError:
        pass
    try:
        return max(0.0, parsedate_to_datetime(value).timestamp() - time.time())
    except (TypeError, ValueError):
        return None
