import { requestClient } from '#/api/request';

export namespace ChatflowApi {
  export interface ChatParams {
    appid: string;
  }

  export interface CompletionsBody {
    userId: string;
    conversationId: string;
    content: string;
    inputs: {
      [key: string]: any;
    };
    files: [];
  }

  export interface ChatListBody {
    userId: string;
    lastId: string;
    sortBy: string;
    limit: string;
    content?: string | undefined;
  }

  export interface ChatListResult {
    data: [
      {
        createAt: string;
        id: string;
        inputs: {
          [key: string]: any;
        };
        introduction: string;
        name: string;
        status: string;
        updatedAt: string;
      },
    ];
    limit: number;
    hasMore: boolean;
  }

  export interface deleteParams {
    userId: string;
    conversationId: string;
    appId: string;
  }

  export interface ChatMessageParams {
    userId: string;
    conversationId: string;
    firstId: string;
    limit: string;
  }

  export interface ChatMessageResult {
    data: [
      {
        answer: string;
        conversationId: string;
        createdAt: string;
        id: string;
        inputs: {
          [key: string]: any;
        };
        messageFiles: [];
        query: string;
      },
    ];
    limit: number;
    hasMore: boolean;
  }
}

// 聊天流
export async function sendChatflow(
  params: ChatflowApi.ChatParams,
  data: ChatflowApi.CompletionsBody,
) {
  return requestClient.post(`/v1/chat/completions/${params.appid}`, data);
}

// word
export async function sendWord(appId: any, data: ChatflowApi.CompletionsBody) {
  return requestClient.post(`word/completions/${appId}`, data);
}

export async function getChatList(
  params: ChatflowApi.ChatParams,
  data: ChatflowApi.ChatListBody,
) {
  const url = params.appid
    ? `/v1/chat/conversations/${params.appid}`
    : '/v1/chat/conversations/';
  return requestClient.request<ChatflowApi.ChatListResult>(url, {
    data,
    method: 'POST',
  });
}

// export function stopChatflow(data){
//   return requestClient.post('/v1/chat/stopMessagesStream', data);
// };
//
// export function getChatflowList(data){
//   return requestClient.post('/v1/chat/messages', data);
// };
//
export function deleteChatflow(data: ChatflowApi.deleteParams) {
  return requestClient.delete('/v1/chat/conversation', { params: data });
}

export function getChatParameters(appId: string) {
  return requestClient.get(`/v1/chat/parameters/${appId}`);
}

export function getChatflowMessage(
  appId: string,
  data: ChatflowApi.ChatMessageParams,
) {
  return requestClient.post<ChatflowApi.ChatMessageResult>(
    `/v1/chat/messages/${appId}`,
    data,
  );
}
