use crate::error::AppError;
use crate::models::notification::Notification;
use crate::AppState;
use tauri::State;

#[tauri::command]
pub fn list_notifications(
    state: State<'_, AppState>,
    project_id: String,
    unread_only: bool,
) -> Result<Vec<Notification>, AppError> {
    let conn = state.db.lock().map_err(|e| AppError::from(e.to_string()))?;
    let notifications = Notification::list_by_project(&conn, &project_id, unread_only)?;
    Ok(notifications)
}

#[tauri::command]
pub fn mark_notification_read(
    state: State<'_, AppState>,
    id: String,
) -> Result<(), AppError> {
    let conn = state.db.lock().map_err(|e| AppError::from(e.to_string()))?;
    Notification::mark_read(&conn, &id)?;
    Ok(())
}

#[tauri::command]
pub fn mark_all_notifications_read(
    state: State<'_, AppState>,
    project_id: String,
) -> Result<usize, AppError> {
    let conn = state.db.lock().map_err(|e| AppError::from(e.to_string()))?;
    let count = Notification::mark_all_read(&conn, &project_id)?;
    Ok(count)
}
