use crate::agent::{AgentConfigStatus, SaveAgentRuntimeConfigRequest, UpdateRemoteApiKeyRequest};
use crate::state::AppState;

/// Return the current public configuration state for the AI chat runtime.
#[tauri::command]
pub async fn get_agent_config_status(
    state: tauri::State<'_, AppState>,
) -> Result<AgentConfigStatus, String> {
    let agent = state.agent.lock().await;

    agent.get_config_status().map_err(|error| error.to_string())
}

/// Persist the non-secret provider and task routing settings.
#[tauri::command]
pub async fn save_agent_runtime_config(
    state: tauri::State<'_, AppState>,
    request: SaveAgentRuntimeConfigRequest,
) -> Result<AgentConfigStatus, String> {
    let mut agent = state.agent.lock().await;

    agent
        .save_runtime_config(request)
        .map_err(|error| error.to_string())
}

/// Save or replace the plaintext remote API key.
#[tauri::command]
pub async fn update_remote_api_key(
    state: tauri::State<'_, AppState>,
    request: UpdateRemoteApiKeyRequest,
) -> Result<AgentConfigStatus, String> {
    let mut agent = state.agent.lock().await;

    agent
        .update_remote_api_key(request)
        .map_err(|error| error.to_string())
}

/// Remove the stored plaintext remote API key.
#[tauri::command]
pub async fn clear_remote_api_key(
    state: tauri::State<'_, AppState>,
) -> Result<AgentConfigStatus, String> {
    let mut agent = state.agent.lock().await;

    agent
        .clear_remote_api_key()
        .map_err(|error| error.to_string())
}
