import json

# Load data
with open('C:\\temp\\req_data.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

req_map = data['reqMap']
pairs = data['pairs']

def get_req(req_id):
    """Get requirement details safely"""
    if req_id in req_map:
        return req_map[req_id]
    return {'id': req_id, 'title': '[NOT FOUND]', 'doc': ''}

def analyze_pair(pair_a, pair_b):
    """Analyze a pair and return confusability score"""
    a = get_req(pair_a)
    b = get_req(pair_b)
    
    a_id = a['id']
    b_id = b['id']
    a_title = a['title'] or ''
    b_title = b['title'] or ''
    a_doc = a['doc'] or ''
    b_doc = b['doc'] or ''
    
    # Combine all text
    a_full = f"{a_id} {a_title} {a_doc}".lower()
    b_full = f"{b_id} {b_title} {b_doc}".lower()
    
    # Scoring logic based on various factors
    
    # 1. Check if same ID prefix (e.g., both REQ-HAZARD-*)
    a_prefix = a_id.rsplit('-', 1)[0]
    b_prefix = b_id.rsplit('-', 1)[0]
    same_prefix = a_prefix == b_prefix
    
    # 2. Check title similarity
    a_words = set(a_title.lower().split())
    b_words = set(b_title.lower().split())
    common_words = a_words & b_words
    
    # 3. Check if docs exist and are similar
    has_both_docs = bool(a_doc and b_doc)
    
    # Scoring rules
    if a_id == b_id:
        return 100, "Same ID"
    
    if not a_title or not b_title:
        # Missing title makes them less confusable
        return 15, "Missing title"
    
    if "hazard" in a_id.lower() and "hazard" in b_id.lower():
        # Both hazard requirements - examine more closely
        if same_prefix:
            # Same hazard family
            if len(common_words) > 3:
                # Many common words = more confusable
                return 65, "Same hazard family, similar titles"
            elif "control" in a_title.lower() and "control" in b_title.lower():
                return 55, "Both about control mechanism"
            elif "respawn" in a_title.lower() or "restart" in a_title.lower():
                if "respawn" in b_title.lower() or "restart" in b_title.lower():
                    return 70, "Both brain restart/respawn"
            else:
                return 35, "Different hazard categories"
        else:
            # Different hazard families
            # Check content similarity
            if len(common_words) > 2 and not {"drop", "file", "direct", "write"}.isdisjoint(common_words):
                return 50, "Hazard about file/write operations"
            return 25, "Different hazard families"
    
    if same_prefix:
        # Same prefix but not hazard
        return 30, f"Same prefix {a_prefix}"
    
    # Domain analysis
    picker_reqs = {"picker", "picker-1", "picker-2", "picker-3", "picker-"}
    endpoint_reqs = {"endpoint", "req-endpoint"}
    run_reqs = {"run", "endpoint-run"}
    adapter_reqs = {"adapter", "req-adapter"}
    install_reqs = {"install", "req-install"}
    resume_reqs = {"resume", "req-resume"}
    msg_reqs = {"msg-", "message"}
    
    a_lower = a_id.lower()
    b_lower = b_id.lower()
    
    def has_domain(req_id, domain_set):
        return any(d in req_id.lower() for d in domain_set)
    
    # Check domain matches
    a_domains = []
    b_domains = []
    for domain in [picker_reqs, endpoint_reqs, run_reqs, adapter_reqs, install_reqs, resume_reqs, msg_reqs]:
        if has_domain(a_lower, domain):
            a_domains.append(str(domain))
        if has_domain(b_lower, domain):
            b_domains.append(str(domain))
    
    if a_domains and b_domains and a_domains == b_domains:
        # Same domain
        if len(common_words) > 3:
            return 45, "Same domain, similar titles"
        else:
            return 28, "Same domain, different purpose"
    
    # Completely different domains
    return 18, "Different domains"

# Score all pairs
results = []
for i, pair in enumerate(pairs):
    a_id = pair['a']
    b_id = pair['b']
    score, reason = analyze_pair(a_id, b_id)
    results.append({
        'pair_index': i + 1,
        'a': a_id,
        'b': b_id,
        'score': score,
        'reason': reason
    })
    if i < 10 or i % 20 == 0:
        print(f"Pair {i+1}: {a_id} <-> {b_id} = {score} ({reason})")

print(f"\nTotal pairs scored: {len(results)}")
avg_score = sum(r['score'] for r in results) / len(results)
print(f"Average score: {avg_score:.1f}")

# Export results
with open('C:\\temp\\scores.json', 'w', encoding='utf-8') as f:
    json.dump(results, f, indent=2)
print("Scores exported to C:\\temp\\scores.json")
