//! 项目资源存储命令

use crate::ApiResponse;
use crate::StringResultExt;
use crate::storage::project as project_storage;

#[tauri::command]
pub async fn save_project_asset(
    app: tauri::AppHandle,
    project_id: String,
    filename: String,
    base64_data: String,
) -> ApiResponse<String> {
    let user_id = match crate::storage::auth::get_current_user_id(&app) {
        Ok(Some(id)) => id,
        Ok(None) => return ApiResponse { code: 401, message: "未登录".to_string(), data: None },
        Err(e) => return ApiResponse { code: 500, message: format!("获取用户信息失败: {}", e), data: None },
    };
    match project_storage::save_project_asset(&user_id, &project_id, &filename, &base64_data).map_err_string() {
        Ok(path) => ApiResponse {
            code: 200,
            message: "资源保存成功".to_string(),
            data: Some(path),
        },
        Err(e) => ApiResponse {
            code: 500,
            message: format!("保存资源失败: {}", e),
            data: None,
        },
    }
}

#[tauri::command]
pub async fn get_video_save_path(
    app: tauri::AppHandle,
    project_id: String,
    filename: String,
) -> ApiResponse<String> {
    let user_id = match crate::storage::auth::get_current_user_id(&app) {
        Ok(Some(id)) => id,
        Ok(None) => return ApiResponse { code: 401, message: "未登录".to_string(), data: None },
        Err(e) => return ApiResponse { code: 500, message: format!("获取用户信息失败: {}", e), data: None },
    };
    match project_storage::get_video_save_path(&user_id, &project_id, &filename).map_err_string() {
        Ok(path) => ApiResponse {
            code: 200,
            message: "获取路径成功".to_string(),
            data: Some(path.to_string_lossy().to_string()),
        },
        Err(e) => ApiResponse {
            code: 500,
            message: format!("获取路径失败: {}", e),
            data: None,
        },
    }
}


