
    Kix                        d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZmZ dd	lmZ 	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 ddZy
)zCTools 13, 14, 15: community listing, detail, architecture overview.    )annotations)Any   )get_architecture_overviewget_communities)node_to_dict)generate_hintsget_session   )
_get_storeNc                   t        |       \  }}	 t        |||      }|dk(  r|D cg c]  }|d   |d   |d   d }}ddt        |       d	|d
}t        d|t	                     |d<   ||j                          S c c}w # t        $ r(}	dt        |	      dcY d}	~	|j                          S d}	~	ww xY w# |j                          w xY w)a  List detected code communities in the codebase.

    [EXPLORE] Retrieves stored communities from the knowledge graph.
    Each community represents a cluster of related code entities
    (functions, classes) detected via the Leiden algorithm or
    file-based grouping.

    Args:
        repo_root: Repository root path. Auto-detected if omitted.
        sort_by: Sort column: size, cohesion, or name.
        min_size: Minimum community size to include (default: 0).
        detail_level: "standard" (default) returns full community data;
                      "minimal" returns only name, size, and cohesion
                      per community.

    Returns:
        List of communities with size and cohesion scores.
    )sort_bymin_sizeminimalnamesizecohesion)r   r   r   okzFound z communities)statussummarycommunitieslist_communities_hintserrorr   r   N)r   r   lenr	   r
   close	Exceptionstr)
	repo_rootr   r   detail_levelstorerootr   cresultexcs
             s/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/code_review_graph/tools/community_tools.pylist_communities_funcr(      s    0 Y'KE4%7X
 9$ % 6AfI1Z=QK 
 K 01>&%

 *
x  	!  6!CH556 	s?   B A<.B <B 	B2
B-B2B5 -B22B5 5Cc                   t        |      \  }}	 d}t        |      }| |D ]  }|j                  d      |k(  s|} n2 n0| .|D ])  }| j                         |d   j                         v s'|} n |ddd|j	                          S |rA|j                  d      }	|	.|j                  |	      }
|
D cg c]  }t        |       }}||d<   dd	|d    d
|d    d|d   d|d}t        d|t                     |d<   ||j	                          S c c}w # t        $ r(}dt        |      dcY d}~|j	                          S d}~ww xY w# |j	                          w xY w)a$  Get details of a single code community.

    [EXPLORE] Retrieves a community by its database ID or by name match.
    Optionally includes the full list of member nodes.

    Args:
        community_name: Name to search for (partial match). Ignored if
                        community_id given.
        community_id: Database ID of the community.
        include_members: If True, include full member node details.
        repo_root: Repository root path. Auto-detected if omitted.

    Returns:
        Community details, or not_found status.
    Nidr   	not_foundz/No community found matching the given criteria.r   r   member_detailsr   zCommunity 'z': r   z nodes, cohesion r   z.4f)r   r   	communityget_communityr   r   r   )r   r   getlowerr   get_nodes_by_community_idr   r	   r
   r   r   )community_namecommunity_idinclude_membersr    r"   r#   r.   all_communitiesr$   cidmember_nodesnmembersr%   r&   s                  r'   get_community_funcr;   G   s   * Y'KE4.!%	)%0#$ 55;,. !I '$ !'')QvY__->> !I
 %E< 	/ --%C$>>sC4@Aq<?AA.5	*+ i/0V$% &%j1#68 #
 *V[]
x  	' B   6!CH556 	sR   (D 0D *D *D 0D9D D 	ED='E(E =EE Ec                   t        |       \  }}	 t        |      }t        |d         }t        |d         }t        |d         }dd| d| d| dd	|}t        d
|t	                     |d<   ||j                          S # t        $ r(}dt        |      dcY d}~|j                          S d}~ww xY w# |j                          w xY w)a  Generate an architecture overview based on community structure.

    [EXPLORE] Builds a high-level view of the codebase architecture by
    analyzing community boundaries and cross-community coupling.
    Includes warnings for high coupling between communities.

    Args:
        repo_root: Repository root path. Auto-detected if omitted.

    Returns:
        Architecture overview with communities, cross-community edges,
        and warnings.
    r   cross_community_edgeswarningsr   zArchitecture: z communities, z cross-community edges, z warning(s)r,   r   r   r   r   N)r   r   r   r	   r
   r   r   r   )	r    r"   r#   overviewn_communitiesn_cross
n_warningsr%   r&   s	            r'   get_architecture_overview_funcrC      s      Y'KE4,U3H]34h678*-.
 ~)3,k+
 
 *'
x  	  6!CH556 	s0   AB   	B1	B,B1B4 ,B11B4 4C)Nr   r   standard)
r    
str | Noner   r   r   intr!   r   returndict[str, Any])NNFN)
r3   rE   r4   z
int | Noner5   boolr    rE   rG   rH   )N)r    rE   rG   rH   )__doc__
__future__r   typingr   r   r   r   graphr   hintsr	   r
   _commonr   r(   r;   rC        r'   <module>rR      s    I "  D   /  !"	... . 	.
 .n "&#! 	DDD D 	D
 DZ !&&&rQ   