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)

# Get the most recent LLM results
raw_objs = sorted(mc.list_objects("stonks-llm-results", recursive=True), key=lambda o: o.last_modified, reverse=True)

for o in raw_objs[:3]:
    data = json.loads(mc.get_object("stonks-llm-results", o.object_name).read())
    attempts = data.get("attempts", [])
    if not attempts:
        continue
    last = attempts[-1]
    raw_out = last.get("raw_output", "")
    ticker = o.object_name.split("/")[1]
    doc_id = o.object_name.split("/")[-2]

    print(f"=== {ticker} / {doc_id[:8]} ===")
    print(f"  success: {data.get('success')}")
    print(f"  duration: {data.get('total_duration_ms')}ms")

    try:
        parsed = json.loads(raw_out)
        print(f"  summary: {parsed.get('summary', '')[:120]}")
        print(f"  confidence: {parsed.get('confidence')}")
        print(f"  companies: {len(parsed.get('companies', []))}")
        print(f"  macro_themes: {parsed.get('macro_themes', [])}")
        for c in parsed.get("companies", []):
            print(f"    -> {c.get('ticker')} sent={c.get('sentiment')} impact={c.get('impact_score')} catalyst={c.get('catalyst_type')}")
    except:
        print(f"  raw output: {raw_out[:200]}")
    print()
