#!/usr/bin/env python3
"""Run commercial prompts through ChatGPT answer-scrape routes and keep the ads blocks.

Usage: run-chatgpt-ads-check.py <out-dir>
Writes <out-dir>/raw/<route>__<country>__<n>.json (raw, gitignored) and
<out-dir>/calls.jsonl (one line per call: route, prompt, country, ok, ads, shopping, sources, ms).
Every call goes through the treg CLI.
"""
import json, subprocess, sys, time
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path

PROMPTS = [
    "best project management software for small teams",
    "which crm should a 10 person sales team buy",
    "best mattress for back pain",
    "cheapest car insurance for a new driver",
    "best vpn for streaming",
    "where to buy flowers for same day delivery",
    "best meal kit delivery service",
    "best running shoes for flat feet",
    "best web hosting for a small business website",
    "online therapy services that take insurance",
    "best credit card for travel rewards",
    "hotels near times square new york",
]

def cloro(prompt, country, legacy):
    body = {"prompt": prompt, "country": country, "legacy": legacy,
            "include": {"markdown": True, "ads": True, "shopping": True}}
    return "cloro.ai-search.chatgpt.scrape", body

def dfs(prompt, country, _):
    return ("dataforseo.x.ai-optimization-chat-gpt-llm-scraper-live-advanced",
            [{"keyword": prompt, "language_code": "en", "location_code": 2840}])

JOBS = []
for i, p in enumerate(PROMPTS):
    JOBS.append(("cloro-desktop", cloro, p, "US", True, i))
    JOBS.append(("cloro-default", cloro, p, "US", False, i))
    JOBS.append(("dataforseo", dfs, p, "US", None, i))
# three prompts again from two more countries on the desktop mode
for i in (0, 4, 10):
    for c in ("GB", "CA"):
        JOBS.append(("cloro-desktop", cloro, PROMPTS[i], c, True, i))

def run(job, out):
    label, fn, prompt, country, legacy, i = job
    ep, body = fn(prompt, country, legacy)
    t = time.time()
    p = subprocess.run(["treg", "call", ep, "--method", "POST", "--data", json.dumps(body)],
                       capture_output=True, text=True, timeout=600)
    ms = int((time.time() - t) * 1000)
    raw = out / "raw" / f"{label}__{country}__{i:02d}.json"
    raw.write_text(p.stdout or p.stderr)
    return {"route": label, "endpoint": ep, "prompt": prompt, "country": country, "n": i,
            "ms": ms, "raw": raw.name}

def main():
    out = Path(sys.argv[1]); (out / "raw").mkdir(parents=True, exist_ok=True)
    with ThreadPoolExecutor(8) as ex:
        rows = list(ex.map(lambda j: run(j, out), JOBS))
    with open(out / "calls.jsonl", "w") as f:
        for r in rows: f.write(json.dumps(r) + "\n")
    print(len(rows), "calls")

if __name__ == "__main__":
    main()
