#!/usr/bin/env python3
"""Read-only bounded census of public publish.new artifact metadata.

Usage: python3 publish_new_sales_census.py --output publish-new-census.json
The script never requests artifact content or purchase endpoints.
"""
import argparse
import datetime as dt
import json
import time
import urllib.request
from pathlib import Path

OWN_SLUGS = {
    "netlify-x402-csv-starter-source-setup-guide-and-ten-tests-84d63ab4",
    "agent-work-market-snapshot-six-venues-get-only-refresh-tool-25-sep-2026-40e95f24",
}


def number(value):
    try:
        return float(value or 0)
    except (TypeError, ValueError):
        return 0.0


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--output", required=True)
    parser.add_argument("--max-pages", type=int, default=80)
    args = parser.parse_args()
    if not 1 <= args.max_pages <= 80:
        parser.error("--max-pages must be between 1 and 80")

    started = dt.datetime.now(dt.timezone.utc)
    raw_path = Path(args.output).with_suffix('.rows.jsonl')
    if raw_path.exists() or Path(args.output).exists():
        parser.error('Output already exists; preserve prior evidence')
    rows, pages, errors = [], [], []
    for page in range(1, args.max_pages + 1):
        if page > 1:
            time.sleep(1.05)
        url = f"https://publish.new/api/artifact?page={page}&limit=100"
        request = urllib.request.Request(url, headers={"User-Agent": "public-metadata-audit/1.0"})
        try:
            with urllib.request.urlopen(request, timeout=20) as response:
                if response.status != 200:
                    raise RuntimeError(f"HTTP {response.status}")
                data = json.load(response)
            artifacts = data.get("artifacts")
            if not isinstance(artifacts, list):
                raise RuntimeError("unexpected list response schema")
            with raw_path.open('a', encoding='utf-8') as raw:
                for artifact in artifacts:
                    raw.write(json.dumps({'page': page, 'artifact': artifact}, ensure_ascii=False) + '\n')
            pages.append({"page": page, "rows": len(artifacts)})
            rows.extend(artifacts)
            if not artifacts:
                break
        except Exception as exc:  # stop on first error; never retry
            errors.append({"page": page, "error_type": type(exc).__name__})
            break

    unique = {}
    for row in rows:
        unique.setdefault(str(row.get("id") or row.get("slug")), row)
    all_rows = list(unique.values())
    positives = [r for r in all_rows if number(r.get("transaction_count")) > 0 or number(r.get("gross_volume")) > 0]

    def safe(row):
        keys = ("id", "slug", "title", "description", "content_type", "published_at",
                "price", "transaction_count", "gross_volume", "review_count", "average_rating")
        return {key: row.get(key) for key in keys}

    positive_rows = [safe(row) for row in positives]
    positive_rows.sort(key=lambda r: (number(r.get("gross_volume")), number(r.get("transaction_count"))), reverse=True)
    dates = [r.get("published_at") for r in all_rows if r.get("published_at")]
    ended = dt.datetime.now(dt.timezone.utc)
    result = {
        "started_at_utc": started.replace(microsecond=0).isoformat().replace("+00:00", "Z"),
        "ended_at_utc": ended.replace(microsecond=0).isoformat().replace("+00:00", "Z"),
        "duration_seconds": round((ended - started).total_seconds(), 1),
        "source": "https://publish.new/api/artifact?page={1..N}&limit=100",
        "method": "Sequential read-only GETs, 1.05 seconds between requests; stop at first HTTP/schema error or empty page.",
        "pages_returned": pages,
        "rows_returned": len(rows),
        "unique_rows": len(all_rows),
        "response_pagination_fields": ["page", "limit"],
        "response_total_or_total_pages": "not exposed",
        "oldest_published_at": min(dates) if dates else None,
        "newest_published_at": max(dates) if dates else None,
        "counter_types_observed": {
            "transaction_count": sorted({type(r.get("transaction_count")).__name__ for r in all_rows if r.get("transaction_count") is not None}),
            "gross_volume": sorted({type(r.get("gross_volume")).__name__ for r in all_rows if r.get("gross_volume") is not None}),
        },
        "positive_counter_rows": len(positives),
        "missing_counter_rows": sum('transaction_count' not in r or 'gross_volume' not in r for r in all_rows),
        "positive_rows_excluding_task_products": sum(r.get("slug") not in OWN_SLUGS for r in positives),
        "positive_rows_all": positive_rows,
        "errors": errors,
        "caveat": "Counters are platform-reported metadata, not independently verified settlement receipts; values may lag or omit payments. Publication time is not sale time. Paging can be affected by concurrent listing changes. No paid content was fetched.",
    }
    with open(args.output, "w", encoding="utf-8") as output:
        json.dump(result, output, indent=2, ensure_ascii=False)
        output.write("\n")


if __name__ == "__main__":
    main()
