"""RAG performance metrics — parses audit.jsonl.

Usage:
    python metrics.py              # Print summary
    python metrics.py --plot       # Print summary + save charts
"""

import json
import sys
from collections import Counter
from pathlib import Path

import yaml


def load_events(config_path: str = "config.yaml") -> list:
    with open(config_path) as f:
        cfg = yaml.safe_load(f)
    log_path = cfg["logging"]["audit_file"]

    if not Path(log_path).exists():
        print(f"No audit log at {log_path}")
        return []

    events = []
    with open(log_path) as f:
        for line in f:
            try:
                events.append(json.loads(line))
            except json.JSONDecodeError:
                continue
    return events


def print_summary(events: list) -> None:
    queries = [e for e in events if e.get("event") == "rag_query"]
    misses = [e for e in events if e.get("hit_count", 1) == 0]
    escalated = [e for e in events if e.get("online_escalated", False)]

    total = len(queries)
    miss_count = len(misses)
    hit_rate = ((total - miss_count) / total * 100) if total > 0 else 0

    print("=" * 60)
    print("SAFECLAW RAG PERFORMANCE METRICS")
    print("=" * 60)
    print(f"Total Queries:    {total}")
    print(f"Hits:             {total - miss_count}")
    print(f"Misses:           {miss_count}")
    print(f"Escalated (Grok): {len(escalated)}")
    print(f"Hit Rate:         {hit_rate:.1f}%")
    print(f"Target:           85%")

    if queries:
        scores = [e.get("top_score", 0) for e in queries]
        print(f"Avg Top Score:    {sum(scores)/len(scores):.4f}")
        print(f"Max Top Score:    {max(scores):.4f}")
        print(f"Min Top Score:    {min(scores):.4f}")

    model_counts = Counter(e.get("model_used", "?") for e in queries)
    print(f"\nBy Model:")
    for model, count in model_counts.items():
        print(f"  {model}: {count}")
    print("=" * 60)


if __name__ == "__main__":
    events = load_events()
    if not events:
        sys.exit(1)
    print_summary(events)
