import { request } from '../../../utils';
import { API } from '../../../types';
import { ResponseType } from '../response-type';

export interface PostSearchUsersReturn extends ResponseType {
  data: {
    user_id: number;
    first_name: string;
    last_name: string;
    avatar: string | null;
  }[];
}

// status: 1 -sent; 2 -received; 3 -read; 4 - deleted
export interface PostGetChatsListReturn extends ResponseType {
  conversations: {
    uid: number;
    name: string;
    avatar: string | null;
    short: string;
    sent_by: number;
    updated: Date;
    status: 1 | 2 | 3 | 4;
    unread_count: number;
    last_message_id: number;
    pin: 0 | 1;
    pin_order: number;
    archive: 0 | 1;
    archive_order: number;
    attachement_name: string;
    encrypted: 0 | 1;
    muted: 0 | 1;
  }[];
}

interface Message {
  id: number;
  sender: number;
  recipient: number;
  text: string;
  status: 1 | 2 | 3 | 4;
  sent_datetime: Date;
  received_datetime: Date | null;
  read_datetime: Date | null;
  reply_to_id: number;
  reactions: string;
  edits: string;
  attachement: any;
  encrypted: 0 | 1;
}

export interface PostGetChatWithReturn extends ResponseType {
  messages: (Message & {
    reply_to: Message;
  })[];
}

export interface PostSendMessage {
  token: string;
  to_uid: number;
  text: string;
  reply_to_id?: number;
}

export interface PostSendMessageReturn extends ResponseType {
  message_id: number;
}

export interface PostMessagesReceivedOrRead {
  token: string;
  from_user: number;
  messages_id: number[];
}

export interface PostReactToMessage {
  token: string;
  message_id: number;
  reaction: string;
  conversation_with_user: number;
}

export interface PostUnreactToMessage {
  token: string;
  message_id: number;
  conversation_with_user: number;
}

export interface PostDeleteMessage {
  token: string;
  message_id: number;
  conversation_with_user: number;
}

export interface PostDeleteChat {
  token: string;
  conversation_with_user: number;
}

export interface PostSetSettings {
  token: string;
  value: 0 | 1;
  conversation_with_user: number;
}

export interface PostGetBlockedReturn extends ResponseType {
  blocked: {
    id: number;
    first_name: string;
    last_name: string;
    avatar: string | null;
  }[];
}

export interface PostGetUnreadCountReturn extends ResponseType {
  unread_conversations: number;
}

export const chatApi = {
  searchUsers: (token: string, search: string) =>
    request.postForm<PostSearchUsersReturn>(API.SEARCH_USERS, { token, search }),
  getChatsList: (token: string, archive: 0 | 1) =>
    request.postForm<PostGetChatsListReturn>(API.GET_CHATS_LIST, { token, archive }),
  getChatWith: (
    token: string,
    uid: number,
    no_of_messages: number,
    previous_than_message_id: number
  ) =>
    request.postForm<PostGetChatWithReturn>(API.GET_CHAT_WITH, {
      token,
      uid,
      no_of_messages,
      previous_than_message_id
    }),
  sendMessage: (data: PostSendMessage) =>
    request.postForm<PostSendMessageReturn>(API.SEND_MESSAGE, data),
  messagesReceived: (data: PostMessagesReceivedOrRead) =>
    request.postForm<ResponseType>(API.MESSAGES_RECEIVED, data),
  messagesRead: (data: PostMessagesReceivedOrRead) =>
    request.postForm<ResponseType>(API.MESSAGES_READ, data),
  reactToMessage: (data: PostReactToMessage) =>
    request.postForm<ResponseType>(API.REACT_TO_MESSAGE, data),
  deleteMessage: (data: PostDeleteMessage) =>
    request.postForm<ResponseType>(API.DELETE_MESSAGE, data),
  setPin: (data: PostSetSettings) => request.postForm<ResponseType>(API.SET_PIN, data),
  setArchive: (data: PostSetSettings) => request.postForm<ResponseType>(API.SET_ARCHIVE, data),
  setBlock: (data: PostSetSettings) => request.postForm<ResponseType>(API.SET_BLOCK, data),
  setMute: (data: PostSetSettings) => request.postForm<ResponseType>(API.SET_MUTE, data),
  deleteChat: (data: PostDeleteChat) => request.postForm<ResponseType>(API.DELETE_CHAT, data),
  unreactToMessage: (data: PostUnreactToMessage) =>
    request.postForm<ResponseType>(API.UNREACT_TO_MESSAGE, data),
  getBlocked: (token: string) => request.postForm<PostGetBlockedReturn>(API.GET_BLOCKED, { token }),
  getUnreadMessagesCount: (token: string) =>
    request.postForm<PostGetUnreadCountReturn>(API.GET_UNREAD_MESSAGES_PRESENT, {
      token
    })
};
