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

#[derive(Debug, Deserialize)]
pub struct BatchImportRequest {
    pub source_folder: String,
}

// Re-export PythonProgress as ImportProgress for backward compatibility
// Note: This is used by frontend TypeScript definitions
#[allow(unused_imports)]
pub use crate::python_executor::PythonProgress as ImportProgress;

#[derive(Debug, Serialize)]
pub struct TemplateInfo {
    pub id: String,
    pub name: String,
    pub description: String,
    pub thumbnail_path: String,
    pub draft_content_path: String,
    pub resources_path: String,
    pub created_at: String,
    pub updated_at: String,
    pub canvas_config: serde_json::Value,
    pub duration: i64,
    pub material_count: i32,
    pub track_count: i32,
    pub tags: Vec<String>,
}



#[tauri::command]
pub async fn batch_import_templates(app: tauri::AppHandle, request: BatchImportRequest) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.template_manager".to_string(),
        "--action".to_string(),
        "batch_import".to_string(),
        "--source_folder".to_string(),
        request.source_folder,
    ];

    execute_python_command(app, &args, None).await
}

#[tauri::command]
pub async fn batch_import_templates_with_progress(
    app: AppHandle,
    request: BatchImportRequest
) -> Result<String, String> {
    let params = &[("--source_folder", request.source_folder.as_str())];

    execute_python_action_with_progress(
        app,
        "python_core.services.template_manager",
        "batch_import",
        params,
        "template-import-progress",
        None,
    ).await
}

#[tauri::command]
pub async fn get_templates(app: tauri::AppHandle) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.template_manager".to_string(),
        "--action".to_string(),
        "get_templates".to_string(),
    ];

    execute_python_command(app, &args, None).await
}

#[tauri::command]
pub async fn get_template(app: tauri::AppHandle, template_id: String) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.template_manager".to_string(),
        "--action".to_string(),
        "get_template".to_string(),
        "--template_id".to_string(),
        template_id,
    ];

    execute_python_command(app, &args, None).await
}

#[tauri::command]
pub async fn delete_template(app: tauri::AppHandle, template_id: String) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.template_manager".to_string(),
        "--action".to_string(),
        "delete_template".to_string(),
        "--template_id".to_string(),
        template_id,
    ];

    execute_python_command(app, &args, None).await
}

#[tauri::command]
pub async fn get_template_detail(app: tauri::AppHandle, template_id: String) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.template_manager".to_string(),
        "--action".to_string(),
        "get_template_detail".to_string(),
        "--template_id".to_string(),
        template_id,
    ];

    execute_python_command(app, &args, None).await
}

// Example: AI Video generation with progress
#[allow(dead_code)]
#[tauri::command]
pub async fn generate_video_with_progress(
    app: AppHandle,
    image_path: String,
    prompt: String,
    output_path: String,
) -> Result<String, String> {
    let params = &[
        ("--image_path", image_path.as_str()),
        ("--prompt", prompt.as_str()),
        ("--output_path", output_path.as_str()),
    ];

    execute_python_action_with_progress(
        app,
        "python_core.ai_video.video_generator",
        "generate",
        params,
        "video-generation-progress",
        None,
    ).await
}
