use tauri::State;
use crate::AppState;
use crate::error::AppError;
use crate::pty::PtyOptions;

#[tauri::command]
#[tracing::instrument(skip(state), fields(shell = ?options.shell))]
pub fn pty_spawn(
    state: State<'_, AppState>,
    options: PtyOptions,
) -> Result<String, AppError> {
    state.pty_manager.spawn(options).map_err(AppError::sidecar)
}

#[tauri::command]
pub fn pty_write(state: State<'_, AppState>, id: String, data: String) -> Result<(), AppError> {
    state.pty_manager.write(&id, &data).map_err(AppError::sidecar)
}

#[tauri::command]
pub fn pty_resize(
    state: State<'_, AppState>,
    id: String,
    cols: u16,
    rows: u16,
) -> Result<(), AppError> {
    state.pty_manager.resize(&id, cols, rows).map_err(AppError::sidecar)
}

#[tauri::command]
#[tracing::instrument(skip(state))]
pub fn pty_kill(state: State<'_, AppState>, id: String) -> Result<(), AppError> {
    state.pty_manager.kill(&id).map_err(AppError::sidecar)
}
