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

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

#[derive(Debug, Serialize, Deserialize)]
pub struct BatchUploadVideoRequest {
    pub source_directory: String,
    pub tags: Option<Vec<String>>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TagsRequest {
    pub segment_id: String,
    pub tags: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SearchTagsRequest {
    pub tags: Vec<String>,
    pub match_all: Option<bool>,
}

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

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

/// 根据视频ID获取片段
#[command]
pub async fn get_segments_by_video_id(app: AppHandle, video_id: String) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.media_manager".to_string(),
        "get_segments_by_video_id".to_string(),
        video_id,
    ];
    
    execute_python_command(app, &args, None).await
}

/// 上传单个视频文件
#[command]
pub async fn upload_video_file(app: AppHandle, request: UploadVideoRequest) -> Result<String, String> {
    let mut args = vec![
        "-m".to_string(),
        "python_core.services.media_manager".to_string(),
        "upload_video_file".to_string(),
        request.source_path,
    ];
    
    if let Some(filename) = request.filename {
        args.push(filename);
    } else {
        args.push("".to_string());
    }
    
    if let Some(tags) = request.tags {
        let tags_json = serde_json::to_string(&tags)
            .map_err(|e| format!("Failed to serialize tags: {}", e))?;
        args.push(tags_json);
    } else {
        args.push("[]".to_string());
    }
    
    execute_python_command(app, &args, None).await
}

/// 批量上传视频文件
#[command]
pub async fn batch_upload_video_files(app: AppHandle, request: BatchUploadVideoRequest) -> Result<String, String> {
    let mut args = vec![
        "-m".to_string(),
        "python_core.services.media_manager".to_string(),
        "batch_upload_video_files".to_string(),
        request.source_directory,
    ];
    
    if let Some(tags) = request.tags {
        let tags_json = serde_json::to_string(&tags)
            .map_err(|e| format!("Failed to serialize tags: {}", e))?;
        args.push(tags_json);
    } else {
        args.push("[]".to_string());
    }
    
    execute_python_command(app, &args, None).await
}

/// 添加片段标签
#[command]
pub async fn add_segment_tags(app: AppHandle, request: TagsRequest) -> Result<String, String> {
    let tags_json = serde_json::to_string(&request.tags)
        .map_err(|e| format!("Failed to serialize tags: {}", e))?;
    
    let args = vec![
        "-m".to_string(),
        "python_core.services.media_manager".to_string(),
        "add_segment_tags".to_string(),
        request.segment_id,
        tags_json,
    ];
    
    execute_python_command(app, &args, None).await
}

/// 移除片段标签
#[command]
pub async fn remove_segment_tags(app: AppHandle, request: TagsRequest) -> Result<String, String> {
    let tags_json = serde_json::to_string(&request.tags)
        .map_err(|e| format!("Failed to serialize tags: {}", e))?;
    
    let args = vec![
        "-m".to_string(),
        "python_core.services.media_manager".to_string(),
        "remove_segment_tags".to_string(),
        request.segment_id,
        tags_json,
    ];
    
    execute_python_command(app, &args, None).await
}

/// 增加片段使用次数
#[command]
pub async fn increment_segment_usage(app: AppHandle, segment_id: String) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.media_manager".to_string(),
        "increment_segment_usage".to_string(),
        segment_id,
    ];
    
    execute_python_command(app, &args, None).await
}

/// 根据标签搜索片段
#[command]
pub async fn get_segments_by_tags(app: AppHandle, request: SearchTagsRequest) -> Result<String, String> {
    let tags_json = serde_json::to_string(&request.tags)
        .map_err(|e| format!("Failed to serialize tags: {}", e))?;
    
    let match_all_json = serde_json::to_string(&request.match_all.unwrap_or(false))
        .map_err(|e| format!("Failed to serialize match_all: {}", e))?;
    
    let args = vec![
        "-m".to_string(),
        "python_core.services.media_manager".to_string(),
        "get_segments_by_tags".to_string(),
        tags_json,
        match_all_json,
    ];
    
    execute_python_command(app, &args, None).await
}

/// 获取热门片段
#[command]
pub async fn get_popular_segments(app: AppHandle, limit: Option<i32>) -> Result<String, String> {
    let mut args = vec![
        "-m".to_string(),
        "python_core.services.media_manager".to_string(),
        "get_popular_segments".to_string(),
    ];
    
    if let Some(limit) = limit {
        args.push(limit.to_string());
    }
    
    execute_python_command(app, &args, None).await
}

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

/// 删除片段
#[command]
pub async fn delete_segment(app: AppHandle, segment_id: String) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.media_manager".to_string(),
        "delete_segment".to_string(),
        segment_id,
    ];
    
    execute_python_command(app, &args, None).await
}

/// 删除原始视频
#[command]
pub async fn delete_original_video(app: AppHandle, video_id: String) -> Result<String, String> {
    let args = vec![
        "-m".to_string(),
        "python_core.services.media_manager".to_string(),
        "delete_original_video".to_string(),
        video_id,
    ];
    
    execute_python_command(app, &args, None).await
}
