use crate::AppState;
use crate::error::AppError;
use crate::search::SearchResult;
use tauri::State;

#[tauri::command]
pub fn search_init(state: State<'_, AppState>) -> Result<(), AppError> {
    // SearchDb is already initialized during app setup; this is a no-op
    // but allows the frontend to confirm readiness.
    let _db = &state.search_db;
    Ok(())
}

#[tauri::command]
pub fn search_query(
    state: State<'_, AppState>,
    query: String,
    limit: Option<i32>,
) -> Result<Vec<SearchResult>, AppError> {
    state.search_db.search_all(&query, limit.unwrap_or(20))
        .map_err(AppError::database)
}

#[tauri::command]
pub fn search_rebuild(state: State<'_, AppState>) -> Result<(), AppError> {
    state.search_db.rebuild_index().map_err(AppError::database)
}

#[tauri::command]
pub fn search_index_message(
    state: State<'_, AppState>,
    session_id: String,
    role: String,
    content: String,
) -> Result<(), AppError> {
    state.search_db.index_message(&session_id, &role, &content)
        .map_err(AppError::database)
}

#[tauri::command]
pub fn search_index_task(
    state: State<'_, AppState>,
    task_id: String,
    title: String,
    description: String,
    status: String,
    assigned_to: String,
) -> Result<(), AppError> {
    state.search_db.index_task(&task_id, &title, &description, &status, &assigned_to)
        .map_err(AppError::database)
}

#[tauri::command]
pub fn search_index_btmsg(
    state: State<'_, AppState>,
    msg_id: String,
    from_agent: String,
    to_agent: String,
    content: String,
    channel: String,
) -> Result<(), AppError> {
    state.search_db.index_btmsg(&msg_id, &from_agent, &to_agent, &content, &channel)
        .map_err(AppError::database)
}
