//! 认证状态存储命令

use crate::ApiResponse;
use crate::StringResultExt;
use crate::storage::auth as auth_storage;

#[tauri::command]
pub async fn load_auth_state(app: tauri::AppHandle) -> ApiResponse<Option<serde_json::Value>> {
    match auth_storage::load_auth_state(&app).map_err_string() {
        Ok(data) => ApiResponse {
            code: 200,
            message: "Auth state loaded successfully".to_string(),
            data: Some(data),
        },
        Err(e) => ApiResponse {
            code: 500,
            message: format!("Failed to load auth state: {}", e),
            data: None,
        },
    }
}

#[tauri::command]
pub async fn save_auth_state(
    app: tauri::AppHandle,
    state: serde_json::Value,
) -> ApiResponse<bool> {
    match auth_storage::save_auth_state(&app, &state).map_err_string() {
        Ok(_) => ApiResponse {
            code: 200,
            message: "Auth state saved successfully".to_string(),
            data: Some(true),
        },
        Err(e) => ApiResponse {
            code: 500,
            message: format!("Failed to save auth state: {}", e),
            data: None,
        },
    }
}

#[tauri::command]
pub async fn clear_auth_state(app: tauri::AppHandle) -> ApiResponse<bool> {
    match auth_storage::clear_auth_state(&app).map_err_string() {
        Ok(_) => ApiResponse {
            code: 200,
            message: "Auth state cleared successfully".to_string(),
            data: Some(true),
        },
        Err(e) => ApiResponse {
            code: 500,
            message: format!("Failed to clear auth state: {}", e),
            data: None,
        },
    }
}
