/**
 * Python Core 演示命令
 * 展示如何使用新的增强版Python Core
 */

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

#[derive(Debug, Serialize, Deserialize)]
pub struct PythonResponse {
    pub status: bool,
    pub msg: String,
    pub data: Option<serde_json::Value>,
}

/// 测试Python Core基础功能
#[command]
pub async fn test_python_core_basic(app: AppHandle) -> Result<PythonResponse, String> {
    println!("Testing Python Core basic functionality...");
    
    let result = execute_python_module_action(
        app,
        "test",
        "hello",
        None,
        None
    ).await?;
    
    // 解析响应
    let response: PythonResponse = serde_json::from_str(&result)
        .map_err(|e| format!("Failed to parse response: {}", e))?;
    
    Ok(response)
}

/// 测试模板管理功能
#[command]
pub async fn test_template_manager(app: AppHandle) -> Result<PythonResponse, String> {
    println!("Testing template manager...");
    
    let result = call_python_service(
        app,
        "template_manager",
        "get_templates",
        None
    ).await?;
    
    Ok(PythonResponse {
        status: true,
        msg: "Template manager test completed".to_string(),
        data: Some(result),
    })
}

/// 测试项目管理功能
#[command]
pub async fn test_project_manager(app: AppHandle) -> Result<PythonResponse, String> {
    println!("Testing project manager...");
    
    let result = call_python_service(
        app,
        "project_manager",
        "get_projects",
        None
    ).await?;
    
    Ok(PythonResponse {
        status: true,
        msg: "Project manager test completed".to_string(),
        data: Some(result),
    })
}

/// 测试文件管理功能
#[command]
pub async fn test_file_manager(app: AppHandle, path: String) -> Result<PythonResponse, String> {
    println!("Testing file manager with path: {}", path);
    
    let params = serde_json::json!({
        "path": path
    });
    
    let result = call_python_service(
        app,
        "file_manager",
        "list_files",
        Some(params)
    ).await?;
    
    Ok(PythonResponse {
        status: true,
        msg: "File manager test completed".to_string(),
        data: Some(result),
    })
}

/// 测试AI视频功能
#[command]
pub async fn test_ai_video(app: AppHandle) -> Result<PythonResponse, String> {
    println!("Testing AI video functionality...");
    
    let result = call_python_service(
        app,
        "ai_video",
        "test_environment",
        None
    ).await?;
    
    Ok(PythonResponse {
        status: true,
        msg: "AI video test completed".to_string(),
        data: Some(result),
    })
}

/// 批量导入模板（演示带参数的调用）
#[command]
pub async fn test_batch_import_templates(
    app: AppHandle, 
    folder_path: String
) -> Result<PythonResponse, String> {
    println!("Testing batch import templates from: {}", folder_path);
    
    let params = serde_json::json!({
        "folder_path": folder_path,
        "force_reimport": false
    });
    
    let result = call_python_service(
        app,
        "template_manager",
        "batch_import",
        Some(params)
    ).await?;
    
    Ok(PythonResponse {
        status: true,
        msg: "Batch import test completed".to_string(),
        data: Some(result),
    })
}

/// 创建新项目（演示复杂参数调用）
#[command]
pub async fn test_create_project(
    app: AppHandle,
    name: String,
    description: Option<String>
) -> Result<PythonResponse, String> {
    println!("Testing create project: {}", name);
    
    let mut params = serde_json::json!({
        "name": name
    });
    
    if let Some(desc) = description {
        params["description"] = serde_json::Value::String(desc);
    }
    
    let result = call_python_service(
        app,
        "project_manager",
        "create_project",
        Some(params)
    ).await?;
    
    Ok(PythonResponse {
        status: true,
        msg: "Create project test completed".to_string(),
        data: Some(result),
    })
}

/// 获取所有资源分类
#[command]
pub async fn test_get_categories(app: AppHandle) -> Result<PythonResponse, String> {
    println!("Testing get categories...");
    
    let result = call_python_service(
        app,
        "resource_category_manager",
        "get_all_categories",
        None
    ).await?;
    
    Ok(PythonResponse {
        status: true,
        msg: "Get categories test completed".to_string(),
        data: Some(result),
    })
}

/// 综合测试 - 测试所有主要功能
#[command]
pub async fn test_all_python_core_functions(app: AppHandle) -> Result<Vec<PythonResponse>, String> {
    println!("Running comprehensive Python Core tests...");
    
    let mut results = Vec::new();
    
    // 测试基础功能
    match test_python_core_basic(app.clone()).await {
        Ok(response) => results.push(response),
        Err(e) => results.push(PythonResponse {
            status: false,
            msg: format!("Basic test failed: {}", e),
            data: None,
        }),
    }
    
    // 测试模板管理
    match test_template_manager(app.clone()).await {
        Ok(response) => results.push(response),
        Err(e) => results.push(PythonResponse {
            status: false,
            msg: format!("Template manager test failed: {}", e),
            data: None,
        }),
    }
    
    // 测试项目管理
    match test_project_manager(app.clone()).await {
        Ok(response) => results.push(response),
        Err(e) => results.push(PythonResponse {
            status: false,
            msg: format!("Project manager test failed: {}", e),
            data: None,
        }),
    }
    
    // 测试AI视频
    match test_ai_video(app.clone()).await {
        Ok(response) => results.push(response),
        Err(e) => results.push(PythonResponse {
            status: false,
            msg: format!("AI video test failed: {}", e),
            data: None,
        }),
    }
    
    // 测试文件管理
    match test_file_manager(app.clone(), ".".to_string()).await {
        Ok(response) => results.push(response),
        Err(e) => results.push(PythonResponse {
            status: false,
            msg: format!("File manager test failed: {}", e),
            data: None,
        }),
    }
    
    // 测试资源分类
    match test_get_categories(app.clone()).await {
        Ok(response) => results.push(response),
        Err(e) => results.push(PythonResponse {
            status: false,
            msg: format!("Categories test failed: {}", e),
            data: None,
        }),
    }
    
    Ok(results)
}
