"""Tradepost V2 read client. Python 3.11+, standard library only.

Set TRADEPOST_V2_BASE_URL to the V2 origin, then run:
  python3 tradepost_v2.py --query vault --dataset market

Public reads need no credential. Private reads require a provisioned V2 token;
Imported V1 credentials remain usable. See /api-docs on your V2 host.
"""
from __future__ import annotations

import argparse
from datetime import datetime, timezone
from decimal import Decimal
from email.utils import parsedate_to_datetime
import http.client
import json
import math
import os
from pathlib import Path
import random
import tempfile
import time
import urllib.error
import urllib.parse
import urllib.request


class APIError(RuntimeError):
    def __init__(self, status, message):
        self.status = status
        super().__init__(f"HTTP {status}: {message}")


class SnapshotChanged(APIError):
    def __init__(self):
        super().__init__(409, "Snapshot changed or was pruned; discard staged pages and restart")


class _NoRedirect(urllib.request.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newurl):
        # Never forward an account credential to another host or endpoint.
        return None


def decimal_or_none(value):
    """Preserve absent prices; never coerce null/empty to zero."""
    if value is None or value == "":
        return None
    result = Decimal(str(value))
    if not result.is_finite():
        raise ValueError("Price must be finite")
    return result


def require_fresh(metadata, max_age_hours=26):
    """Validate source collection time, not HTTP/cache/publication time."""
    if not math.isfinite(max_age_hours) or max_age_hours <= 0:
        raise ValueError("max_age_hours must be positive")
    timestamp = metadata.get("market_snapshot_at") or metadata.get("collected_at")
    if not timestamp:
        raise ValueError("Source collection timestamp is missing")
    collected = datetime.fromisoformat(timestamp.replace("Z", "+00:00"))
    if collected.tzinfo is None:
        raise ValueError("Source collection timestamp needs a timezone")
    age = (datetime.now(timezone.utc) - collected).total_seconds() / 3600
    if age < -5 / 60 or age > max_age_hours:
        raise ValueError(f"Source age {age:.2f}h is outside the accepted window")
    return age


class TradepostV2:
    def __init__(self, base_url, token=None, timeout=30, attempts=3):
        base = urllib.parse.urlsplit(base_url)
        local = base.hostname in {"localhost", "127.0.0.1", "::1"}
        if (not base.hostname or base.username or base.password or base.query or
                base.fragment or base.path not in {"", "/"} or
                not (base.scheme == "https" or (local and base.scheme == "http"))):
            raise ValueError("Use an HTTPS origin only (HTTP allowed for localhost)")
        if timeout <= 0 or not 1 <= attempts <= 5:
            raise ValueError("Use a positive timeout and 1–5 attempts")
        self.base_url = base_url.rstrip("/")
        self.token, self.timeout, self.attempts = token, timeout, attempts
        self.opener = urllib.request.build_opener(_NoRedirect())

    def _url(self, path, params):
        if not path.startswith("/api/v2/") or any(c in path for c in "?#\\") or "/../" in path:
            raise ValueError("Use an /api/v2/ path and pass query parameters separately")
        values = {k: v for k, v in (params or {}).items() if v is not None}
        return self.base_url + path + ("?" + urllib.parse.urlencode(values) if values else "")

    def _request(self, path, params, accept, consume):
        headers = {"Accept": accept, "User-Agent": "Tradepost-V2-Bot-Example/1.0"}
        if path.startswith("/api/v2/account/"):
            if not self.token:
                raise ValueError("Private account reads require a provisioned V2 token")
            headers["Authorization"] = "Bearer " + self.token
        request = urllib.request.Request(self._url(path, params), headers=headers)
        for attempt in range(self.attempts):
            retry_after = None
            try:
                with self.opener.open(request, timeout=self.timeout) as response:
                    return consume(response)
            except urllib.error.HTTPError as error:
                retry_after = error.headers.get("Retry-After")
                raw = error.read(4096)
                error.close()
                try:
                    message = json.loads(raw).get("error", "Request failed")
                except (ValueError, AttributeError):
                    message = "Request failed; check the base URL and endpoint"
                failure = SnapshotChanged() if error.code == 409 else APIError(error.code, message)
                # V2 has no application quota, but infrastructure may return 429.
                if error.code not in {429, 502, 503, 504} or attempt + 1 == self.attempts:
                    raise failure from None
            except (urllib.error.URLError, TimeoutError, ConnectionError, http.client.HTTPException) as error:
                failure = APIError(0, "Network read failed")
                if attempt + 1 == self.attempts:
                    raise failure from error
            delay = 2 ** attempt + random.random() * 0.25
            if retry_after:
                try:
                    delay = max(delay, float(retry_after))
                except ValueError:
                    try:
                        delay = max(delay, parsedate_to_datetime(retry_after).timestamp() - time.time())
                    except (ValueError, TypeError, OverflowError):
                        pass
            if delay > 30:
                # Let the bot scheduler reschedule; do not retry before Retry-After.
                raise failure
            time.sleep(max(0, delay))

    def get(self, path, **params):
        if path.startswith("/api/v2/export/"):
            raise ValueError("Use download_csv for bulk exports; do not buffer the whole catalog")

        def decode(response):
            if response.headers.get_content_type() != "application/json":
                raise APIError(response.status, "Expected JSON; check the origin and format")
            return json.load(response, parse_float=Decimal)

        return self._request(path, params, "application/json", decode)

    def pages(self, dataset, **filters):
        """Yield validated public pages. Stage all pages before updating bot state."""
        if dataset not in {"market", "retail"} or "format" in filters:
            raise ValueError("Choose market/retail and JSON pagination")
        params = {"limit": 500, "offset": 0, **filters}
        version, count = params.get("version"), None
        while True:
            data = self.get(f"/api/v2/{dataset}/products", **params)
            if version is not None and data["version"] != version:
                raise SnapshotChanged()
            version = data["version"]
            if count is not None and data["count"] != count:
                raise APIError(200, "Count changed inside the pinned snapshot")
            count = data["count"]
            offset, rows, next_offset = data["offset"], data["rows"], data["next_offset"]
            if offset != int(params["offset"]) or data["returned"] != len(rows):
                raise APIError(200, "Invalid pagination response")
            if next_offset is not None and (not rows or next_offset != offset + len(rows)):
                raise APIError(200, "Pagination did not advance correctly")
            if next_offset is None and offset + len(rows) < count:
                raise APIError(200, "Incomplete final page")
            yield data
            if next_offset is None:
                return
            params.update(version=version, offset=next_offset)

    def account_pages(self, route, **filters):
        """Account APIs cannot pin versions: abort if the snapshot changes."""
        if route not in {"orders", "payments", "transfers", "addresses"}:
            raise ValueError("Use get() for account status or the combined activity endpoint")
        params = {"view": "redacted", "limit": 100, "offset": 0, **filters}
        version, count = None, None
        while True:
            data = self.get(f"/api/v2/account/{route}", **params)
            if version is not None and data["snapshot_version"] != version:
                raise SnapshotChanged()
            version = data["snapshot_version"]
            paging, rows = data["pagination"], data["rows"]
            if count is not None and paging["total_count"] != count:
                raise APIError(200, "Account count changed inside a snapshot")
            count = paging["total_count"]
            next_offset = paging["next_offset"]
            if (paging["offset"] != int(params["offset"]) or
                    paging["has_next_page"] != (next_offset is not None) or
                    (next_offset is not None and (not rows or next_offset != paging["offset"] + len(rows))) or
                    (next_offset is None and paging["offset"] + len(rows) < count)):
                raise APIError(200, "Invalid account pagination")
            yield data
            if next_offset is None:
                return
            params["offset"] = next_offset

    def download_csv(self, dataset, destination, **filters):
        """Stream a complete export to a temporary file, then atomically replace."""
        if dataset not in {"market", "retail"} or any(k in filters for k in ("limit", "offset", "format")):
            raise ValueError("Choose market/retail; bulk exports have no pagination parameters")
        destination = Path(destination)
        fd, temporary = tempfile.mkstemp(prefix=destination.name + ".", suffix=".part", dir=destination.parent)
        os.close(fd)
        try:
            def save(response):
                if response.headers.get_content_type() != "text/csv":
                    raise APIError(response.status, "Expected CSV")
                with open(temporary, "wb") as output:
                    written = 0
                    while chunk := response.read(64 * 1024):
                        output.write(chunk)
                        written += len(chunk)
                    length = response.headers.get("Content-Length")
                    if length is not None and written != int(length):
                        raise http.client.IncompleteRead(b"", max(0, int(length) - written))
            self._request(f"/api/v2/export/{dataset}", {**filters, "format": "csv"}, "text/csv", save)
            os.replace(temporary, destination)
        finally:
            if os.path.exists(temporary):
                os.unlink(temporary)


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--dataset", choices=["market", "retail"], default="market")
    parser.add_argument("--query", default="vault")
    args = parser.parse_args()
    client = TradepostV2(os.environ["TRADEPOST_V2_BASE_URL"])
    status = client.get("/api/v2/status")
    meta = status["datasets"][args.dataset]
    require_fresh(meta)
    filters = {"q": args.query, "min_sales": 1, "version": meta["version"]}
    if args.dataset == "retail":
        filters["view"] = "comparisons"
    staged = []
    for page in client.pages(args.dataset, **filters):
        require_fresh(page)
        staged.extend(page["rows"])
    print(json.dumps({"version": meta["version"], "count": len(staged), "rows": staged}, default=str, indent=2))


if __name__ == "__main__":
    main()
