bl_info = {
    "name": "Select Exact",
    "author": "Grok + Jonathan",
    "version": (1, 1),
    "blender": (5, 2, 0),
    "location": "View3D > N-panel > Select Exact",
    "description": "Select objects with exact same vertex/edge/face count in the active object's collection",
    "category": "Object",
}

import bpy

class OBJECT_OT_select_exact(bpy.types.Operator):
    bl_idname = "object.select_exact"
    bl_label = "Select Exact Matches"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        active = context.active_object
        if not active or active.type != 'MESH':
            self.report({'WARNING'}, "No active mesh object selected")
            return {'CANCELLED'}

        # Get stats from active
        active_verts = len(active.data.vertices)
        active_edges = len(active.data.edges)
        active_faces = len(active.data.polygons)

        # Search in active object's collection
        collection = active.users_collection[0] if active.users_collection else bpy.context.collection
        objects = [obj for obj in collection.objects if obj.type == 'MESH']

        matches = []
        for obj in objects:
            if obj == active:
                continue
            if (len(obj.data.vertices) == active_verts and
                len(obj.data.edges) == active_edges and
                len(obj.data.polygons) == active_faces):
                matches.append(obj)

        # Select matches + active
        for obj in matches:
            obj.select_set(True)
        active.select_set(True)

        count = len(matches)
        if count == 0:
            self.report({'INFO'}, "Only found the selected object")
        else:
            self.report({'INFO'}, f"Found {count} exact matches")

        return {'FINISHED'}


class VIEW3D_PT_select_exact(bpy.types.Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Select Exact"
    bl_label = "Select Exact"

    def draw(self, context):
        layout = self.layout
        col = layout.column()
        
        col.operator("object.select_exact", text="Select Exact Matches")
        
        # Info note
        box = col.box()
        box.label(text="Selects exact matches in")
        box.label(text="the active object's collection")


def register():
    bpy.utils.register_class(OBJECT_OT_select_exact)
    bpy.utils.register_class(VIEW3D_PT_select_exact)


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_select_exact)
    bpy.utils.unregister_class(VIEW3D_PT_select_exact)


if __name__ == "__main__":
    register()