import qs from 'qs';

import api from 'src/config/api';
import { CHAT_CONSTANTS } from 'src/constants/chats';
import {
  ChatListResponse,
  ChatByIdResponse,
  ChatMessagesResponse,
  CreateGroupChatResponse,
  EditGroupChatResponse,
  ChatsNotificationsResponse,
  Message,
  GroupChatData,
  PosibleParticipantsResponse,
  PaginatedChatResponse,
  BaseChatListItem,
} from 'src/types/chats';
import { ReactionUsersResponse } from 'src/types/reaction';

export const getChatList = (page, types, q = '', limit = 20) =>
  api.get<ChatListResponse>('chats', { params: { page, limit, q, types } });

export const getArchivedChatList = (page, types, q = '', limit = 20) =>
  api.get<PaginatedChatResponse>('chats/paginated', {
    params: { page, limit, q, types, archived: true },
  });

export const getAllChatables = (page, q = '', limit = 20) =>
  api.get('chatables/potential-recipients', { params: { page, limit, q } });

export const archiveChat = ({ chatId, userId, archiveState }) =>
  api.patch<ChatListResponse>(`/users/${userId}/chats/${chatId}`, {
    archived: archiveState,
  });

export const markAsUnread = ({ chatId, userId }) =>
  api.patch<ChatListResponse>(`/users/${userId}/chats/${chatId}`, {
    markedAsUnread: true,
  });

export const getChatById = id => api.get<ChatByIdResponse>(`chats/${id}/alt`);

export const getChatMessages = (
  id,
  page,
  limit = CHAT_CONSTANTS.MESSAGES_LIMIT_PER_PAGE,
) =>
  api.get<ChatMessagesResponse>(`chats/${id}/messages`, {
    params: { page, limit },
  });

export const sendMessage = (id, body) =>
  api.post<Message>(`chats/${id}/messages`, body);

export const createChat = (userId: number) =>
  api.post<BaseChatListItem>('chats/one-to-one-chats', { userId });

export const exitGroupChat = (id: number | string, userId: number) =>
  api.delete(`chats/group-chats/${id}/users/${userId}`);

export const createGroupChat = (body: GroupChatData) =>
  api.post<CreateGroupChatResponse>('chats/group-chats', body);

export const updateGroupChatTitle = (id, title: string) =>
  api.put<EditGroupChatResponse>(`chats/group-chats/${id}/title`, { title });

export const updateGroupChatPictureUrl = (id, pictureUrl: string) =>
  api.put<EditGroupChatResponse>(`chats/group-chats/${id}/picture-url`, {
    pictureUrl,
  });

export const addUsersToGroupChat = (id, userIds: number[]) =>
  api.post(`chats/group-chats/${id}/users`, { userIds });

export const removeUserFromGroupChat = (id, userId: number) =>
  api.patch(`chats/group-chats/${id}/users`, { removeUserIds: [userId] });

export const makeAdminOfGroupChat = (id, userId: number) =>
  api.post(`/chats/group-chats/${id}/admins`, { userIds: [userId] });

export const getGroupList = (q, page, limit = 100, types = 'REGULAR_GROUP') =>
  q
    ? api.get<ChatListResponse>('/chats', { params: { page, limit, q, types } })
    : api.get<ChatListResponse>('/chats', { params: { page, limit, types } });

// Messages

export const forwardMessage = (chatId, messageId, body) =>
  api.post(`/chats/${chatId}/messages/${messageId}/forward`, { ...body });

export const deleteMessage = (chatId, messageId) =>
  api.delete(`/chats/${chatId}/messages/${messageId}`);

export const editMessage = (chatId, messageId, body) =>
  api.patch(`/chats/${chatId}/messages/${messageId}`, body);

export const getMessageReactionUsers = (id, messageId, emoji) =>
  api.get<ReactionUsersResponse>(
    `/chats/${id}/messages/${messageId}/reactions/users`,
    { params: { emoji } },
  );

export const addMessageReaction = (id, messageId, emoji, unified) =>
  api.post(`/chats/${id}/messages/${messageId}/reactions/add`, {
    emoji,
    unified,
  });

export const removeMessageReaction = async (id, messageId, emoji) =>
  api.post(`/chats/${id}/messages/${messageId}/reactions/remove`, { emoji });

export const getParticipantsOfGroup = (
  id,
  page = undefined,
  limit = undefined,
  filterSelf = false,
  includeDeleted = true,
) =>
  api.get(`/chats/${id}/users`, {
    params: { page, limit, filterSelf, includeDeleted },
  });

export const getPosibleParticipants = (
  id,
  search: string,
  page: number = 0,
  limit: number = 100,
) =>
  api.get<PosibleParticipantsResponse>(`/chats/${id}/possible-participants`, {
    params: {
      page,
      limit,
      search: search || undefined,
    },
    paramsSerializer: params => qs.stringify(params),
  });

// Notifications
export const getChatsNotifications = () =>
  api.get<ChatsNotificationsResponse>('red-bubbles/chats');

export const clearChatsNotifications = () =>
  api.post('red-bubbles/chats/clear');

export const clearChatNotifications = (id: number | string) =>
  api.post(`red-bubbles/chats/${id}/clear`);
