"""Grounding DINO judge: zero-shot object detection from prompt keywords"""
import requests, json, io
from PIL import Image

GDINO_URL = "http://localhost:5051/search"
DEFAULT_UUID = "aeed71342a899fe4b4c57b7d41bcb692"

def score(frames, prompt, start_time=0, end_time=0):
    prompt_lower = prompt.lower()
    
    # Time-bounded search using GDINO range (format "start-end")
    search_range = f"{int(start_time)}-{int(end_time) if end_time > start_time else int(start_time) + 60}"
    
    try:
        resp = requests.post(GDINO_URL, json={
            "uuid": DEFAULT_UUID,
            "query": prompt_lower,
            "range": search_range,
            "interval": 10,
            "threshold": 0.1,
        }, timeout=60)
        data = resp.json()
        hits = data.get("hits", [])
        n_hits = len(hits)
        best_score = max((h.get("best_score", 0) for h in hits), default=0)
        dets_found = []
        for h in hits:
            for d in h.get("detections", []):
                dets_found.append(d.get("label", ""))
        
        score_val = int(100 * min(1.0, best_score * 2))
        
        return {
            "agent": "GroundingDINO",
            "score": score_val,
            "reasoning": f"{n_hits} hits, range={search_range}, best={best_score:.2f}",
            "details": {"n_hits": n_hits, "best_score": best_score}
        }
    except Exception as e:
        return {"agent": "GroundingDINO", "score": 50, "reasoning": f"GDINO error: {str(e)[:80]}", "details": {}}
