use serde::{Deserialize, Serialize};
use tauri::AppHandle;
use crate::utils::python_cli::execute_python_cli_command;

#[derive(Debug, Serialize, Deserialize)]
pub struct ModelResponse {
    pub success: bool,
    pub data: Option<serde_json::Value>,
    pub error: Option<String>,
    pub message: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct ModelCreateRequest {
    pub model_number: String,
    pub model_image: String,
    pub user_id: Option<String>,
    pub is_cloud: Option<bool>,
    pub verbose: Option<bool>,
    pub json_output: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct ModelListRequest {
    pub user_id: Option<String>,
    pub include_cloud: Option<bool>,
    pub include_inactive: Option<bool>,
    pub limit: Option<i32>,
    pub offset: Option<i32>,
    pub verbose: Option<bool>,
    pub json_output: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct ModelGetRequest {
    pub model_id: String,
    pub verbose: Option<bool>,
    pub json_output: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct ModelUpdateRequest {
    pub model_id: String,
    pub model_number: Option<String>,
    pub model_image: Option<String>,
    pub is_active: Option<bool>,
    pub is_cloud: Option<bool>,
    pub verbose: Option<bool>,
    pub json_output: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct ModelDeleteRequest {
    pub model_id: String,
    pub hard_delete: Option<bool>,
    pub verbose: Option<bool>,
    pub json_output: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct ModelSearchRequest {
    pub query: String,
    pub user_id: Option<String>,
    pub include_cloud: Option<bool>,
    pub limit: Option<i32>,
    pub verbose: Option<bool>,
    pub json_output: Option<bool>,
}

/// 创建模特
#[tauri::command]
pub async fn create_model_cli(
    app: AppHandle,
    request: ModelCreateRequest,
) -> Result<ModelResponse, String> {
    let mut args = vec![
        "model".to_string(),
        "create".to_string(),
        request.model_number,
        request.model_image,
    ];

    if let Some(user_id) = request.user_id {
        args.push("--user-id".to_string());
        args.push(user_id);
    }

    if request.is_cloud.unwrap_or(false) {
        args.push("--cloud".to_string());
    }

    if request.verbose.unwrap_or(false) {
        args.push("--verbose".to_string());
    }

    if request.json_output.unwrap_or(true) {
        args.push("--json".to_string());
    }

    execute_python_cli_command(app, args).await
}

/// 获取模特列表
#[tauri::command]
pub async fn list_models_cli(
    app: AppHandle,
    request: ModelListRequest,
) -> Result<ModelResponse, String> {
    let mut args = vec!["model".to_string(), "list".to_string()];

    if let Some(user_id) = request.user_id {
        args.push("--user-id".to_string());
        args.push(user_id);
    }

    if request.include_cloud.unwrap_or(true) {
        args.push("--include-cloud".to_string());
    }

    if request.include_inactive.unwrap_or(false) {
        args.push("--include-inactive".to_string());
    }

    if let Some(limit) = request.limit {
        args.push("--limit".to_string());
        args.push(limit.to_string());
    }

    if let Some(offset) = request.offset {
        args.push("--offset".to_string());
        args.push(offset.to_string());
    }

    if request.verbose.unwrap_or(false) {
        args.push("--verbose".to_string());
    }

    if request.json_output.unwrap_or(true) {
        args.push("--json".to_string());
    }

    execute_python_cli_command(app, args).await
}

/// 获取模特详情
#[tauri::command]
pub async fn get_model_cli(
    app: AppHandle,
    request: ModelGetRequest,
) -> Result<ModelResponse, String> {
    let mut args = vec![
        "model".to_string(),
        "get".to_string(),
        request.model_id,
    ];

    if request.verbose.unwrap_or(false) {
        args.push("--verbose".to_string());
    }

    if request.json_output.unwrap_or(true) {
        args.push("--json".to_string());
    }

    execute_python_cli_command(app, args).await
}

/// 更新模特
#[tauri::command]
pub async fn update_model_cli(
    app: AppHandle,
    request: ModelUpdateRequest,
) -> Result<ModelResponse, String> {
    let mut args = vec![
        "model".to_string(),
        "update".to_string(),
        request.model_id,
    ];

    if let Some(model_number) = request.model_number {
        args.push("--model-number".to_string());
        args.push(model_number);
    }

    if let Some(model_image) = request.model_image {
        args.push("--model-image".to_string());
        args.push(model_image);
    }

    if let Some(is_active) = request.is_active {
        args.push("--active".to_string());
        args.push(is_active.to_string());
    }

    if let Some(is_cloud) = request.is_cloud {
        args.push("--cloud".to_string());
        args.push(is_cloud.to_string());
    }

    if request.verbose.unwrap_or(false) {
        args.push("--verbose".to_string());
    }

    if request.json_output.unwrap_or(true) {
        args.push("--json".to_string());
    }

    execute_python_cli_command(app, args).await
}

/// 删除模特
#[tauri::command]
pub async fn delete_model_cli(
    app: AppHandle,
    request: ModelDeleteRequest,
) -> Result<ModelResponse, String> {
    let mut args = vec![
        "model".to_string(),
        "delete".to_string(),
        request.model_id,
    ];

    if request.hard_delete.unwrap_or(false) {
        args.push("--hard".to_string());
    }

    if request.verbose.unwrap_or(false) {
        args.push("--verbose".to_string());
    }

    if request.json_output.unwrap_or(true) {
        args.push("--json".to_string());
    }

    execute_python_cli_command(app, args).await
}

/// 搜索模特
#[tauri::command]
pub async fn search_models_cli(
    app: AppHandle,
    request: ModelSearchRequest,
) -> Result<ModelResponse, String> {
    let mut args = vec![
        "model".to_string(),
        "search".to_string(),
        request.query,
    ];

    if let Some(user_id) = request.user_id {
        args.push("--user-id".to_string());
        args.push(user_id);
    }

    if request.include_cloud.unwrap_or(true) {
        args.push("--include-cloud".to_string());
    }

    if let Some(limit) = request.limit {
        args.push("--limit".to_string());
        args.push(limit.to_string());
    }

    if request.verbose.unwrap_or(false) {
        args.push("--verbose".to_string());
    }

    if request.json_output.unwrap_or(true) {
        args.push("--json".to_string());
    }

    execute_python_cli_command(app, args).await
}
