#!/usr/bin/env python3
"""Render one URL through the browser sidecar and say what came back.

Sources of truth: app/services/collectors/fetch.py (render). This is the probe the
brief names for a source we have not collected yet: one permitted page, our honest
identity, robots read fresh, the floor delay, and a plain report of status, the
challenge header if any, the title, how much text was drawn, which JSON calls the
page made for itself and which hosts were aborted. It never clicks anything and it
stops on a refusal; a SourceBlocked here is the answer, not an obstacle.

    ../.venv-dev/bin/python scripts/render-probe.py https://host/path [--allow cdn.host]
        [--sidecar http://127.0.0.1:18080] [--save /path/to/rendered.html]

Announce the run in .logs/runs/ first; it is a network request to a retailer.
"""

import argparse
import logging
import pathlib
import sys

sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))

from app.services.collectors.fetch import (  # noqa: E402
    FetchError,
    SourceBlocked,
    render,
    render_budget,
)
from app.services.collectors.robots import RobotsUnavailable  # noqa: E402


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("url")
    ap.add_argument("--allow", action="append", default=[], help="asset host the page may also reach")
    ap.add_argument("--sidecar", default=None, help="sidecar base URL (default: settings.browser_url)")
    ap.add_argument("--save", default=None, help="write the rendered HTML here")
    ap.add_argument("--delay", type=float, default=10.0)
    ap.add_argument("--wait-until", default="load", help="load (client-rendered) or domcontentloaded (server-rendered)")
    args = ap.parse_args()
    logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")

    try:
        with render_budget(1, "probe"):
            page = render(
                args.url, delay=args.delay, allow_hosts=args.allow,
                wait_until=args.wait_until, sidecar_url=args.sidecar,
            )
    except SourceBlocked as exc:
        print(f"REFUSED: {exc}")
        return 3
    except (FetchError, RobotsUnavailable) as exc:
        print(f"ERROR: {exc}")
        return 2

    print(f"status={page.status} final_url={page.final_url}")
    print(f"headers={page.headers} timed_out={page.timed_out}")
    print(f"title={page.title!r} text_length={page.text_length} html_bytes={len(page.html)} elapsed_ms={page.elapsed_ms}")
    print(f"aborted_hosts={sorted(page.aborted)} disallowed_paths={sorted(page.aborted_paths)}")
    print(f"api_responses={len(page.api_responses)}")
    for r in page.api_responses[:40]:
        print(f"  {r.status} {len(r.body):>8}B {r.url[:140]}")
    if args.save:
        pathlib.Path(args.save).write_text(page.html)
        print(f"saved {args.save}")
    return 0


if __name__ == "__main__":
    sys.exit(main())
