//! Built-in Tools for LLM Function Calling — Phase-83 (T83.2 ~ T83.6) + Phase-91
//!
//! Provides database-aware tools that LLMs can invoke via Function Calling:
//!
//! - `db_vector_search` — Vector similarity search on collections
//! - `db_graph_query` — Graph traversal & relationship queries
//! - `db_sql_query` — Federation store query (read-only)
//! - `db_fts_search` — Full-text search via Tantivy / BM25
//! - `db_doc_crud` — Document CRUD operations
//! - `db_list_sources` — Dynamic data source discovery
//! - `db_tree_search` — Tree-indexed document search (Phase-91)
//!
//! Each tool implements `ToolExecutor` and can be registered in `ToolRegistry`.
//!
//! **Feature gates**: This module requires both `llm-gateway` and `federation` features.

mod vector_search;
mod graph_query;
mod sql_query;
mod fts_search;
mod doc_crud;
mod schema_gen;
mod tree_search;

pub use vector_search::VectorSearchTool;
pub use graph_query::GraphQueryTool;
pub use sql_query::SqlQueryTool;
pub use fts_search::FtsSearchTool;
pub use doc_crud::DocCrudTool;
pub use schema_gen::{ListSourcesTool, SchemaIntrospector, DataSourceSchema, CollectionSchema, StoreSchema};
pub use tree_search::TreeSearchTool;

/// Register all built-in database tools into the given registry.
///
/// This is a convenience function that creates and registers all built-in tools.
///
/// # Arguments
/// * `registry` — The `ToolRegistry` to register tools into
/// * `collections` — The collection store (for vector search, FTS, doc CRUD)
/// * `graph` — Optional graph store (for graph queries)
/// * `federation` — The federation engine (for SQL queries)
pub fn register_all(
    registry: &super::tool_executor::ToolRegistry,
    collections: std::sync::Arc<crate::storage::collection::CollectionStore>,
    graph: Option<std::sync::Arc<crate::social::SocialStoreV2>>,
    federation: std::sync::Arc<crate::federation::Federation>,
) {
    // T83.2: Vector search
    registry.register(VectorSearchTool::new(collections.clone()));

    // T83.3: Graph query
    if let Some(graph) = graph {
        registry.register(GraphQueryTool::new(graph));
    }

    // T83.4: SQL query
    registry.register(SqlQueryTool::new(federation.clone()));

    // T83.5: Full-text search + Document CRUD
    registry.register(FtsSearchTool::new(collections.clone()));
    registry.register(DocCrudTool::new(collections.clone()));

    // T83.6: Data source discovery
    registry.register(ListSourcesTool::new(collections.clone(), federation.clone()));

    // Update registry context with schema info
    let introspector = SchemaIntrospector::new(collections, federation);
    introspector.update_tool_definitions(registry);
}

/// Register the tree search tool separately (Phase-91).
///
/// Called independently because `TreeIndexStore` is optional and may not
/// always be available. Also supports optional `DynamicTreeManager` (Phase-95).
///
/// # Arguments
/// * `registry` — The `ToolRegistry` to register the tool into
/// * `tree_store` — The tree index store
/// * `dynamic_manager` — Optional dynamic tree manager for access tracking
pub fn register_tree_search(
    registry: &super::tool_executor::ToolRegistry,
    tree_store: std::sync::Arc<crate::tree_index::store::TreeIndexStore>,
    dynamic_manager: Option<std::sync::Arc<crate::tree_index::dynamic::DynamicTreeManager>>,
) {
    let mut tool = TreeSearchTool::new(tree_store);
    if let Some(manager) = dynamic_manager {
        tool = tool.with_dynamic_manager(manager);
    }
    registry.register(tool);
}
