//! 封面形象管理 IPC 命令

use crate::ApiResponse;
use crate::storage::cover_avatar as cover_avatar_storage;

// --------------------- 封面形象库命令 ---------------------

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CoverAvatarArgs {
    pub id: String,
    pub name: String,
    pub image_url: String,
    pub local_path: Option<String>,
    pub created_at: String,
}

/// 加载封面形象库
#[tauri::command]
pub async fn load_cover_avatars() -> ApiResponse<Vec<cover_avatar_storage::CoverAvatar>> {
    match cover_avatar_storage::load_cover_avatars() {
        Ok(list) => ApiResponse {
            code: 200,
            message: "封面形象库加载成功".to_string(),
            data: Some(list.avatars),
        },
        Err(e) => ApiResponse {
            code: 500,
            message: format!("加载封面形象库失败: {}", e),
            data: Some(vec![]),
        },
    }
}

/// 保存封面形象
#[tauri::command]
pub async fn save_cover_avatar(
    args: CoverAvatarArgs,
) -> ApiResponse<bool> {
    let avatar = cover_avatar_storage::CoverAvatar {
        id: args.id,
        name: args.name,
        image_url: args.image_url,
        local_path: args.local_path,
        created_at: args.created_at,
    };
    match cover_avatar_storage::add_cover_avatar(avatar) {
        Ok(_) => ApiResponse {
            code: 200,
            message: "封面形象保存成功".to_string(),
            data: Some(true),
        },
        Err(e) => ApiResponse {
            code: 500,
            message: format!("保存封面形象失败: {}", e),
            data: Some(false),
        },
    }
}

/// 删除封面形象
#[tauri::command]
pub async fn delete_cover_avatar_cmd(
    id: String,
) -> ApiResponse<bool> {
    match cover_avatar_storage::delete_cover_avatar(&id) {
        Ok(_) => ApiResponse {
            code: 200,
            message: "封面形象删除成功".to_string(),
            data: Some(true),
        },
        Err(e) => ApiResponse {
            code: 500,
            message: format!("删除封面形象失败: {}", e),
            data: Some(false),
        },
    }
}

// --------------------- 封面形象图片文件命令 ---------------------

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SaveCoverAvatarImageArgs {
    pub avatar_id: String,
    pub image_data: String, // base64 编码
    pub ext: String,        // 文件扩展名，如 "png"
}

/// 保存封面形象图片文件（前端传入 base64 编码）
#[tauri::command]
pub async fn save_cover_avatar_image(
    args: SaveCoverAvatarImageArgs,
) -> ApiResponse<String> {
    let image_bytes = match base64::Engine::decode(
        &base64::engine::general_purpose::STANDARD,
        &args.image_data,
    ) {
        Ok(data) => data,
        Err(e) => return ApiResponse {
            code: 400,
            message: format!("Invalid base64 data: {}", e),
            data: None,
        },
    };

    match cover_avatar_storage::save_cover_avatar_image(
        &args.avatar_id,
        &image_bytes,
        &args.ext,
    ) {
        Ok(path) => ApiResponse {
            code: 200,
            message: "封面形象图片保存成功".to_string(),
            data: Some(path),
        },
        Err(e) => ApiResponse {
            code: 500,
            message: format!("保存封面形象图片失败: {}", e),
            data: None,
        },
    }
}
