import json,subprocess,collections,time,os
UA="Aethos-Agentic-Probe/1.0 (+https://aethoshq.com/agent-probe; contact: hello@aethoshq.com)"
PROFILE="https://aethoshq.com/.well-known/ucp-agent-profile.json"
Q=["luxury handbag","car floor mats","aquarium filter","baby stroller","electric guitar",
   "wedding dress","gaming keyboard","fishing rod","essential oil diffuser","hair clippers"]
def search(q,limit=50):
    body=json.dumps({"jsonrpc":"2.0","method":"tools/call","id":1,"params":{"name":"search_catalog","arguments":{
        "meta":{"ucp-agent":{"profile":PROFILE}},"catalog":{"query":q,"pagination":{"limit":limit}}}}})
    return subprocess.run(["curl","-sS","-m","60","-X","POST","https://catalog.shopify.com/api/ucp/mcp",
        "-H","Content-Type: application/json","-H","Accept: application/json, text/event-stream",
        "-H",f"User-Agent: {UA}","-d",body],capture_output=True,text=True).stdout
def cnt(v):
    if v is None: return None
    if isinstance(v,list): return len(v)
    if isinstance(v,str): return len([p for p in v.split('\n') if p.strip()])
    return -1
rows=[]
for q in Q:
    fn=f"raw/f-{q.replace(' ','-')}.json"
    raw=open(fn).read() if os.path.exists(fn) else search(q)
    if not os.path.exists(fn): open(fn,'w').write(raw); time.sleep(3)
    try: d=json.loads(raw)
    except Exception: print(q,"FAIL"); continue
    for p in d.get('result',{}).get('structuredContent',{}).get('products',[]):
        m=p.get('metadata') or {}
        s=None
        for v in p.get('variants',[]):
            sl=v.get('seller')
            if isinstance(sl,dict): s=sl.get('domain'); break
        rows.append({'q':q,'id':p.get('id'),'title':p.get('title'),'seller':s,
                     'usp':cnt(m.get('unique_selling_points')),'feat':cnt(m.get('top_features')),
                     'spec':cnt(m.get('tech_specs')),'keys':sorted(m.keys())})
    print(f"{q!r}: {len(d.get('result',{}).get('structuredContent',{}).get('products',[]))}")
json.dump(rows,open('falsify.json','w'),indent=1)
u={r['id']:r for r in rows}; rows=list(u.values())
print("\nUNIQUE:",len(rows),"SELLERS:",len({r['seller'] for r in rows if r['seller']}))
print("usp:",dict(sorted(collections.Counter(r['usp'] for r in rows).items(),key=lambda x:(x[0] is None,x[0]))))
print("feat:",dict(sorted(collections.Counter(r['feat'] for r in rows).items(),key=lambda x:(x[0] is None,x[0]))))
print("spec:",dict(sorted(collections.Counter(r['spec'] for r in rows).items(),key=lambda x:(x[0] is None,x[0]))))
print("keysets:",collections.Counter(tuple(r['keys']) for r in rows))
print("COUNTEREXAMPLES:",[(r['seller'],r['title'][:40],r['usp']) for r in rows if r['usp'] not in (1,None)])
