export enum MESSAGE_CONVERSATION_TYPES {
    "USER_TO_USER" = "user-to-user",
    "USER_TO_ORG" = "user-to-org",
}

export type FindMessageConversationsParams = {
    _id?: string;
    userIdFrom?: string;
    userIdTo?: string;
    companyId?: string;
    sessionUser: string;
};

export type PaginateMessageConversationsParams = {
    query?: string;
    userIdFrom?: string;
    userIdTo?: string;
    companyId?: string;
    page: number;
    limit: number;
    sessionUser: string;
};

export type PaginateMessageConversationsResults = {
    data: IMessageConversation[];
    page: number;
    pages: number;
};

export type FindConversationsResults = {
    data: FindConversationItem[];
    page: number;
    pages: number;
};

export type CreateMessageConversationParams = {
    userIdFrom?: string;
    userIdTo?: string;
    companyId?: string;
    type: MESSAGE_CONVERSATION_TYPES;
    sessionUser: string;
};

export type DeleteMessageConversationParams = {
    id: string;
    sessionUser: string;
};

export interface IMessageConversation {
    id?: string;
    userIdFrom?: string; //Indica quien envía el mensaje
    userIdTo?: string; // Indica a que usuario va dirigido el mensaje.
    //                    Cuando el mensaje va dirigido a una organizacion,
    //                    habrá un colaborador de la organizacion que de una respuesta
    //                    y ese id de usuario se almacenara en userIdTo.
    companyId?: string; //Indica que el mensaje va dirigido a una organizacion.
    type: MESSAGE_CONVERSATION_TYPES;
    newMessages: number; //Indica si hay nuevos mensajes en la conversacion.
}

export interface FindConversationItem {
    conversation: IMessageConversation;
    name: string;
    description: string;
    image: string;
    orderWeight: number;
}

export type PaginateFindContactsParams = {
    query?: string;
    page: number;
    limit: number;
    sessionUser: string;
};

export type PaginateFindContactsResults = {
    data: FindConversationItem[];
    page: number;
    pages: number;
};

export type MarkConversationAsReadParams = {
    conversationId: string;
    sessionUser: string;
};

export type CountUnreadConversationParams = {
    conversationId: string;
    sessionUser: string;
};

export type ConvertU2UMessageConversationParams = {
    conversationId: string;
    sessionUser: string;
};

export type DeleteConversationsByCompanyParams = {
    companyId: string;
};

export type DeleteConversationsByUserParams = {
    userId: string;
};

export interface IMessageConversationsAdapter {
    create(data: CreateMessageConversationParams): Promise<IMessageConversation>;
    delete(id: string): Promise<void>;
    find(
        filters: Omit<FindMessageConversationsParams, "sessionUser">
    ): Promise<IMessageConversation[]>;
    findOne(
        filters: Omit<FindMessageConversationsParams, "sessionUser">
    ): Promise<IMessageConversation | null>;
    paginate(
        filters: PaginateMessageConversationsParams
    ): Promise<PaginateMessageConversationsResults>;
}

export interface IMessageConversationsManager {
    conversations: IMessageConversationsAdapter;
    createConversation(data: CreateMessageConversationParams): Promise<IMessageConversation>;
    deleteConversation(data: DeleteMessageConversationParams): Promise<void>;
    paginateConversations(
        data: PaginateMessageConversationsParams
    ): Promise<PaginateMessageConversationsResults>;
    paginateFindContacts(data: PaginateFindContactsParams): Promise<PaginateFindContactsResults>;
    findConversations(data: PaginateFindContactsParams): Promise<FindConversationsResults>;
    markConversationAsRead(data: MarkConversationAsReadParams): Promise<void>;
    countUnreadMessages(data: CountUnreadConversationParams): Promise<number>;
    convertUserToUser(data: ConvertU2UMessageConversationParams): Promise<void>;
    deleteConversationsByCompany(data: DeleteConversationsByCompanyParams): Promise<void>;
    deleteConversationsByUser(data: DeleteConversationsByUserParams): Promise<void>;
}
