#!/usr/bin/env python3
"""
GRAPH-BASED INTENT ROUTING
No keywords. Traverse concept graph.
"""
import sqlite3
import re

DB_PATH = "/Eden/DATA/semantic_reasoning.db"

def get_domain_from_graph(text: str) -> tuple[str, float]:
    """
    Query concept graph to find domain.
    Returns (domain, confidence) or (None, 0)
    """
    # Extract words
    words = set(re.findall(r'\b\w+\b', text.lower()))
    
    conn = sqlite3.connect(DB_PATH)
    c = conn.cursor()
    
    domain_scores = {}
    
    for word in words:
        # Check if word is a concept in any domain
        c.execute("""
            SELECT domain_a, strength FROM cross_domain_links 
            WHERE concept_a = ? OR concept_b = ?
        """, (word, word))
        
        for row in c.fetchall():
            domain, strength = row
            domain_scores[domain] = domain_scores.get(domain, 0) + strength
    
    conn.close()
    
    if not domain_scores:
        return None, 0.0
    
    # Return highest scoring domain
    best_domain = max(domain_scores, key=domain_scores.get)
    confidence = min(domain_scores[best_domain] / 5.0, 1.0)  # Normalize
    
    return best_domain, confidence


def test():
    tests = [
        "how do planets stay in orbit",
        "what happens if I drop a ball",
        "I feel happy today",
        "what is recursion",
        "tell me about gravity"
    ]
    
    for t in tests:
        domain, conf = get_domain_from_graph(t)
        print(f"'{t}' → {domain} ({conf:.2f})")


if __name__ == "__main__":
    test()
