import numpy as np
from sklearn.metrics import silhouette_score
from scipy.sparse.csgraph import shortest_path
from sklearn.neighbors import kneighbors_graph

def SA_dif(embeddings, labels, phate_op):
    """Normalized Semantic Affinity (Diffusion) via PHATE"""
    # Using the diffusion potential from the already-fitted PHATE operator
    diff_potential = phate_op.diff_potential
    score = silhouette_score(diff_potential, labels, metric='precomputed')
    return (score + 1) / 2  # Renormalize to [0, 1]

def SA_cen(embeddings):
    """Single-cluster Centroid Affinity (Conceptual Tightness)"""
    from sklearn.metrics.pairwise import cosine_similarity
    centroid = np.mean(embeddings, axis=0).reshape(1, -1)
    affinities = cosine_similarity(embeddings, centroid)
    return np.mean(affinities)