/**
 * @description AI对话历史相关API
 * @author AI Assistant
 * @since 2025-11-04
 */

import { api } from '@/apis/index';
import type {
  AiConversation,
  ConversationSearchParams,
  MessageSearchParams,
  ResultDomain,
  PageDomain
} from '@/types';

/**
 * 对话历史API服务
 */
export const chatHistoryApi = {
  /**
   * 分页查询用户的会话列表
   * @param params 搜索参数
   * @returns Promise<PageDomain<AiConversation>>
   */
  async pageUserConversations(params: ConversationSearchParams): Promise<PageDomain<AiConversation>> {
    // 后端直接返回PageDomain，不需要再次包装
    const response = await api.post('/ai/chat/history/conversations/page', params);
    return response.data as PageDomain<AiConversation>;
  },

  /**
   * 搜索会话（全文搜索标题和摘要）
   * @param params 搜索参数
   * @returns Promise<PageDomain<AiConversation>>
   */
  async searchConversations(params: MessageSearchParams): Promise<PageDomain<AiConversation>> {
    // 后端直接返回PageDomain，不需要再次包装
    const response = await api.post('/ai/chat/history/search', params);
    return response.data as PageDomain<AiConversation>;
  },

  /**
   * 收藏/取消收藏会话
   * @param conversationId 会话ID
   * @param isFavorite 是否收藏
   * @returns Promise<ResultDomain<boolean>>
   */
  async toggleFavorite(conversationId: string, isFavorite: boolean): Promise<ResultDomain<boolean>> {
    const response = await api.put<boolean>('/ai/chat/history/conversation/favorite', {
      conversationId,
      isFavorite
    });
    return response.data;
  },

  /**
   * 置顶/取消置顶会话
   * @param conversationId 会话ID
   * @param isPinned 是否置顶
   * @returns Promise<ResultDomain<boolean>>
   */
  async togglePin(conversationId: string, isPinned: boolean): Promise<ResultDomain<boolean>> {
    const response = await api.put<boolean>('/ai/chat/history/conversation/pin', {
      conversationId,
      isPinned
    });
    return response.data;
  },

  /**
   * 批量删除会话
   * @param conversationIds 会话ID列表
   * @returns Promise<ResultDomain<number>>
   */
  async batchDeleteConversations(conversationIds: string[]): Promise<ResultDomain<number>> {
    const response = await api.delete<number>('/ai/chat/history/conversations/batch', {
      data: { conversationIds }
    });
    return response.data;
  },

  /**
   * 获取用户的对话统计信息
   * @param userId 用户ID（可选，默认当前用户）
   * @returns Promise<ResultDomain<any>>
   */
  async getUserChatStatistics(userId?: string): Promise<ResultDomain<any>> {
    const response = await api.get<any>('/ai/chat/history/statistics', {
      params: { userId }
    });
    return response.data;
  },

  /**
   * 获取会话的详细统计
   * @param conversationId 会话ID
   * @returns Promise<ResultDomain<any>>
   */
  async getConversationStatistics(conversationId: string): Promise<ResultDomain<any>> {
    const response = await api.get<any>(`/ai/chat/history/conversation/${conversationId}/statistics`);
    return response.data;
  },

  /**
   * 导出会话记录（Markdown格式）
   * @param conversationId 会话ID
   * @returns Promise<ResultDomain<string>>
   */
  async exportConversationAsMarkdown(conversationId: string): Promise<ResultDomain<string>> {
    const response = await api.get<string>(`/ai/chat/history/export/markdown/${conversationId}`);
    return response.data;
  },

  /**
   * 导出会话记录（JSON格式）
   * @param conversationId 会话ID
   * @returns Promise<ResultDomain<string>>
   */
  async exportConversationAsJson(conversationId: string): Promise<ResultDomain<string>> {
    const response = await api.get<string>(`/ai/chat/history/export/json/${conversationId}`);
    return response.data;
  },

  /**
   * 获取用户最近的对话
   * @param limit 数量限制（默认10）
   * @returns Promise<ResultDomain<AiConversation>>
   */
  async getRecentConversations(limit = 10): Promise<ResultDomain<AiConversation>> {
    const response = await api.get<AiConversation>('/ai/chat/history/recent', {
      limit 
    });
    return response.data;
  }
};

