use tauri::State;
use crate::AppState;
use crate::error::AppError;
use crate::fs_watcher::FsWatcherStatus;

#[tauri::command]
pub fn file_watch(
    app: tauri::AppHandle,
    state: State<'_, AppState>,
    pane_id: String,
    path: String,
) -> Result<String, AppError> {
    state.file_watcher.watch(&app, &pane_id, &path).map_err(AppError::filesystem)
}

#[tauri::command]
pub fn file_unwatch(state: State<'_, AppState>, pane_id: String) {
    state.file_watcher.unwatch(&pane_id);
}

#[tauri::command]
pub fn file_read(state: State<'_, AppState>, path: String) -> Result<String, AppError> {
    state.file_watcher.read_file(&path).map_err(AppError::filesystem)
}

#[tauri::command]
pub fn fs_watch_project(
    app: tauri::AppHandle,
    state: State<'_, AppState>,
    project_id: String,
    cwd: String,
) -> Result<(), AppError> {
    state.fs_watcher.watch_project(&app, &project_id, &cwd).map_err(AppError::filesystem)
}

#[tauri::command]
pub fn fs_unwatch_project(state: State<'_, AppState>, project_id: String) {
    state.fs_watcher.unwatch_project(&project_id);
}

#[tauri::command]
pub fn fs_watcher_status(state: State<'_, AppState>) -> FsWatcherStatus {
    state.fs_watcher.status()
}
