import json
import os

from minio import Minio

mc = Minio(os.environ["MINIO_ENDPOINT"], access_key=os.environ["MINIO_ACCESS_KEY"], secret_key=os.environ["MINIO_SECRET_KEY"], secure=False)
raw_objs = list(mc.list_objects("stonks-llm-results", recursive=True))
print(f"LLM result objects: {len(raw_objs)}")
for o in raw_objs[:5]:
    data = json.loads(mc.get_object("stonks-llm-results", o.object_name).read())
    success = data.get("success", False)
    attempts = data.get("attempts", [])
    ticker = o.object_name.split("/")[1] if "/" in o.object_name else "?"
    if attempts:
        last = attempts[-1]
        raw_out = last.get("raw_output", "")
        print(f"  {ticker}: success={success} output_len={len(raw_out)}")
        try:
            parsed = json.loads(raw_out)
            companies = parsed.get("companies", [])
            summary = parsed.get("summary", "")[:80]
            conf = parsed.get("confidence", "?")
            print(f"    summary: {summary}")
            print(f"    confidence: {conf}")
            print(f"    companies: {len(companies)}")
            for c in companies[:3]:
                print(f"      {c.get('ticker','?')} sentiment={c.get('sentiment','?')} impact={c.get('impact_score','?')}")
        except:
            print(f"    raw: {raw_out[:120]}")
    print()
