import { api } from '@/api/index'
import { API_BASE_URL } from '@/config'
import type { ResultDomain } from '@/types'
import type { TbChat, TbChatMessage, ChatPrepareData, StopChatParam, CommentMessageParam, DifyFileInfo } from '@/types/ai'

/**
 * @description    AI对话相关接口
 * @filename    aichat.ts
 * @author    yslg
 * @copyright xyzh
 * @since 2025-12-19
 */
export const aiChatAPI = {
    baseUrl: '/urban-lifeline/ai/chat',

    // ====================== 会话管理 ======================

    /**
     * 创建会话
     * @param chat 会话信息
     */
    async createChat(chat: TbChat): Promise<ResultDomain<TbChat>> {
        const response = await api.post<TbChat>(`${this.baseUrl}/conversation`, chat)
        return response.data
    },

    /**
     * 更新会话
     * @param chat 会话信息
     */
    async updateChat(chat: TbChat): Promise<ResultDomain<TbChat>> {
        const response = await api.put<TbChat>(`${this.baseUrl}/conversation`, chat)
        return response.data
    },

    /**
     * 删除会话
     * @param chat 会话信息
     */
    async deleteChat(chat: TbChat): Promise<ResultDomain<TbChat>> {
        const response = await api.delete<TbChat>(`${this.baseUrl}/conversation`,  chat )
        return response.data
    },

    /**
     * 获取会话列表
     * @param filter 筛选条件
     */
    async getChatList(filter: TbChat): Promise<ResultDomain<TbChat>> {
        const response = await api.post<TbChat>(`${this.baseUrl}/conversation/list`, filter)
        return response.data
    },

    // ====================== 消息管理 ======================

    /**
     * 获取对话消息列表
     * @param filter 筛选条件
     */
    async getMessageList(filter: TbChat): Promise<ResultDomain<TbChatMessage>> {
        const response = await api.post<TbChatMessage>(`${this.baseUrl}/messages`, filter)
        return response.data
    },

    // ====================== 流式对话 ======================

    /**
     * 准备流式对话会话数据
     * @param chatPrepareData 对话预处理数据
     */
    async prepareStreamChat(chatPrepareData: ChatPrepareData): Promise<ResultDomain<string>> {
        const response = await api.post<string>(`${this.baseUrl}/stream/prepare`, chatPrepareData)
        return response.data
    },

    /**
     * 获取流式对话SSE URL
     * @param sessionId 会话ID
     */
    getStreamChatUrl(sessionId: string): string {
        return `${API_BASE_URL}${this.baseUrl}/stream?sessionId=${sessionId}`
    },

    /**
     * 创建流式对话 EventSource
     * @param sessionId 会话ID
     */
    createStreamChat(sessionId: string): EventSource {
        const url = this.getStreamChatUrl(sessionId)
        return new EventSource(url)
    },

    /**
     * 停止对话
     * @param params 停止参数
     */
    async stopChat(params: StopChatParam): Promise<ResultDomain<boolean>> {
        const response = await api.post<boolean>(`${this.baseUrl}/stop`, params)
        return response.data
    },

    /**
     * 评价消息
     * @param params 评价参数
     */
    async commentMessage(params: CommentMessageParam): Promise<ResultDomain<boolean>> {
        const response = await api.post<boolean>(`${this.baseUrl}/comment`, params)
        return response.data
    },

    // ====================== 文件上传 ======================

    /**
     * 上传文件用于对话（图文多模态）
     * @param file 文件
     * @param agentId 智能体ID
     */
    async uploadFileForChat(file: File, agentId: string): Promise<ResultDomain<DifyFileInfo>> {
        const formData = new FormData()
        formData.append('file', file)
        formData.append('agentId', agentId)
        const response = await api.upload<DifyFileInfo>(`${this.baseUrl}/file/upload`, formData)
        return response.data
    }
}