use tauri::State;
use crate::AppState;
use crate::error::AppError;
use crate::{ctx, memora};

// --- ctx commands ---

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

#[tauri::command]
pub fn ctx_register_project(state: State<'_, AppState>, name: String, description: String, work_dir: Option<String>) -> Result<(), AppError> {
    state.ctx_db.register_project(&name, &description, work_dir.as_deref())
        .map_err(AppError::database)
}

#[tauri::command]
pub fn ctx_get_context(state: State<'_, AppState>, project: String) -> Result<Vec<ctx::CtxEntry>, AppError> {
    state.ctx_db.get_context(&project).map_err(AppError::database)
}

#[tauri::command]
pub fn ctx_get_shared(state: State<'_, AppState>) -> Result<Vec<ctx::CtxEntry>, AppError> {
    state.ctx_db.get_shared().map_err(AppError::database)
}

#[tauri::command]
pub fn ctx_get_summaries(state: State<'_, AppState>, project: String, limit: i64) -> Result<Vec<ctx::CtxSummary>, AppError> {
    state.ctx_db.get_summaries(&project, limit).map_err(AppError::database)
}

#[tauri::command]
pub fn ctx_search(state: State<'_, AppState>, query: String) -> Result<Vec<ctx::CtxEntry>, AppError> {
    state.ctx_db.search(&query).map_err(AppError::database)
}

// --- Memora commands (read-only) ---

#[tauri::command]
pub fn memora_available(state: State<'_, AppState>) -> bool {
    state.memora_db.is_available()
}

#[tauri::command]
pub fn memora_list(
    state: State<'_, AppState>,
    tags: Option<Vec<String>>,
    limit: Option<i64>,
    offset: Option<i64>,
) -> Result<memora::MemoraSearchResult, AppError> {
    state.memora_db.list(tags, limit.unwrap_or(50), offset.unwrap_or(0))
        .map_err(AppError::database)
}

#[tauri::command]
pub fn memora_search(
    state: State<'_, AppState>,
    query: String,
    tags: Option<Vec<String>>,
    limit: Option<i64>,
) -> Result<memora::MemoraSearchResult, AppError> {
    state.memora_db.search(&query, tags, limit.unwrap_or(50))
        .map_err(AppError::database)
}

#[tauri::command]
pub fn memora_get(state: State<'_, AppState>, id: i64) -> Result<Option<memora::MemoraNode>, AppError> {
    state.memora_db.get(id).map_err(AppError::database)
}
