use serde::{Deserialize, Serialize};
use tauri::{command, AppHandle};
use crate::python_executor::execute_python_command;

#[derive(Debug, Serialize, Deserialize)]
pub struct UploadAudioRequest {
    pub source_path: String,
    pub filename: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BatchUploadRequest {
    pub source_directory: String,
}

/// 获取所有音频文件
#[command]
pub async fn get_all_audio_files(app: AppHandle) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.audio_manager".to_string(),
        "get_all_audio_files".to_string(),
    ];
    
    execute_python_command(app, &args, None).await
}

/// 根据ID获取音频文件
#[command]
pub async fn get_audio_by_id(app: AppHandle, audio_id: String) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.audio_manager".to_string(),
        "get_audio_by_id".to_string(),
        audio_id,
    ];
    
    execute_python_command(app, &args, None).await
}

/// 根据MD5获取音频文件
#[command]
pub async fn get_audio_by_md5(app: AppHandle, md5_hash: String) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.audio_manager".to_string(),
        "get_audio_by_md5".to_string(),
        md5_hash,
    ];
    
    execute_python_command(app, &args, None).await
}

/// 上传单个音频文件
#[command]
pub async fn upload_audio_file(app: AppHandle, request: UploadAudioRequest) -> Result<String, String> {
    let mut args = vec![
        "-m".to_string(),
        "python_core.services.audio_manager".to_string(),
        "upload_audio_file".to_string(),
        request.source_path,
    ];
    
    if let Some(filename) = request.filename {
        args.push(filename);
    }
    
    execute_python_command(app, &args, None).await
}

/// 批量上传音频文件
#[command]
pub async fn batch_upload_audio_files(app: AppHandle, request: BatchUploadRequest) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.audio_manager".to_string(),
        "batch_upload_audio_files".to_string(),
        request.source_directory,
    ];
    
    execute_python_command(app, &args, None).await
}

/// 删除音频文件
#[command]
pub async fn delete_audio_file(app: AppHandle, audio_id: String) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.audio_manager".to_string(),
        "delete_audio_file".to_string(),
        audio_id,
    ];
    
    execute_python_command(app, &args, None).await
}

/// 搜索音频文件
#[command]
pub async fn search_audio_files(app: AppHandle, keyword: String) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.audio_manager".to_string(),
        "search_audio_files".to_string(),
        keyword,
    ];
    
    execute_python_command(app, &args, None).await
}
