import { http } from "@/shared/api/httpClient";
import { API_ROUTES } from "@/shared/constants/api-routes";

import {
  CreateAllChatsResponseSchema,
  CreateChatResponseSchema,
  GetChatMessagesResponseSchema,
  GetChatsListResponseSchema,
  ICreateAllChatsResponse,
  ICreateChatResponse,
  IGetChatMessagesResponse,
  IGetChatsListResponse,
} from "./types";

export const createAllChats = async (): Promise<ICreateAllChatsResponse> => {
  return http.post<ICreateAllChatsResponse>(
    API_ROUTES.createAllChats(),
    {},
    {
      tags: ["chats", "create-all"],
      schema: CreateAllChatsResponseSchema,
      revalidate: 0,
    }
  );
};

export const createChat = async (
  assistantId: string
): Promise<ICreateChatResponse> => {
  return http.post<ICreateChatResponse>(
    API_ROUTES.createChat(assistantId),
    {},
    {
      tags: ["chats", "create"],
      schema: CreateChatResponseSchema,
      revalidate: 0,
    }
  );
};

export const getChatsList = async (): Promise<IGetChatsListResponse> => {
  return http.get<IGetChatsListResponse>(API_ROUTES.getChatsList(), {
    tags: ["chats-list"],
    schema: GetChatsListResponseSchema,
    revalidate: 0,
  });
};

export const getChatMessages = async (
  chatId: string,
  params: { limit?: number; page?: number } = { limit: 100, page: 1 }
): Promise<IGetChatMessagesResponse> => {
  return http.get<IGetChatMessagesResponse>(
    API_ROUTES.getChatMessages(chatId),
    {
      tags: ["chats", chatId, "messages"],
      schema: GetChatMessagesResponseSchema,
      query: { limit: params.limit, page: params.page },
      revalidate: 0,
    }
  );
};
