// server/src/search_schema.rs

use std::path::Path;
use tantivy::Index;

// Re-export the functions from the common crate.
// This makes them available as `crate::search_schema::create_search_schema`, etc.
pub use common::search::{create_search_schema, register_slovak_tokenizers};

/// Gets an existing index or creates a new one.
/// This function now uses the shared logic from the `common` crate.
pub fn get_or_create_index(table_name: &str) -> tantivy::Result<Index> {
    let index_path = Path::new("./tantivy_indexes").join(table_name);
    std::fs::create_dir_all(&index_path)?;

    let index = if index_path.join("meta.json").exists() {
        Index::open_in_dir(&index_path)?
    } else {
        let schema = create_search_schema();
        Index::create_in_dir(&index_path, schema)?
    };

    // This now calls the single, authoritative function from `common`.
    register_slovak_tokenizers(&index)?;
    Ok(index)
}
