import { connect } from "mongoose";
import MessagesList from "../../Models/Messages/Messages";
import {
    DeleteMessageParams,
    ISendMessageResult,
    PaginateMessageParams,
    PaginateMessageResults,
    SendMessageParams,
} from "../../Models/Messages/Messages.Interface";
import {
    FindConversationsResults,
    MarkConversationAsReadParams,
    PaginateFindContactsParams,
    PaginateFindContactsResults,
} from "../../Models/MessageConversations/MessagesConversations.Interface";
import MessageConversationList from "../../Models/MessageConversations/MessagesConversations";

export class MessagesService {
    public async paginateMessages(data: PaginateMessageParams): Promise<PaginateMessageResults> {
        await connect(`${process.env.DATABASE_CONNECTION}`);

        const messages = await MessagesList.paginateMessages(data);

        return messages;
    }

    public async sendMessage(data: SendMessageParams): Promise<ISendMessageResult> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        return await MessagesList.sendMessage(data);
    }

    public async deleteMessage(data: DeleteMessageParams): Promise<void> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        await MessagesList.deleteMessage(data);
    }

    public async paginateFindConversations(
        data: PaginateFindContactsParams
    ): Promise<FindConversationsResults> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        return await MessageConversationList.findConversations(data);
    }

    public async paginateFindContacts(
        data: PaginateFindContactsParams
    ): Promise<PaginateFindContactsResults> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        return await MessageConversationList.paginateFindContacts(data);
    }

    public async markConversationAsRead(data: MarkConversationAsReadParams): Promise<void> {
        await connect(`${process.env.DATABASE_CONNECTION}`);
        await MessageConversationList.markConversationAsRead(data);
    }
}
