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))

# Find extractions with companies and check their summaries
for o in raw_objs:
    data = json.loads(mc.get_object("stonks-llm-results", o.object_name).read())
    if not data.get("success"):
        continue
    attempts = data.get("attempts", [])
    if not attempts:
        continue
    raw_out = attempts[-1].get("raw_output", "")
    try:
        parsed = json.loads(raw_out)
        companies = parsed.get("companies", [])
        summary = parsed.get("summary", "")
        conf = parsed.get("confidence", 0)
        # Show high-confidence ones that still have no companies
        if conf >= 0.5 and not companies:
            ticker = o.object_name.split("/")[1]
            print(f"HIGH CONF ({conf}) NO COMPANIES - {ticker}:")
            print(f"  summary: {summary[:120]}")
            print()
    except:
        pass
