package com.example.petstoremobile.repositories;

import androidx.lifecycle.LiveData;

import com.example.petstoremobile.api.ChatApi;
import com.example.petstoremobile.api.CustomerApi;
import com.example.petstoremobile.api.MessageApi;
import com.example.petstoremobile.dtos.ConversationDTO;
import com.example.petstoremobile.dtos.CustomerDTO;
import com.example.petstoremobile.dtos.MessageDTO;
import com.example.petstoremobile.dtos.PageResponse;
import com.example.petstoremobile.dtos.SendMessageRequest;
import com.example.petstoremobile.dtos.UpdateConversationStatusRequest;
import com.example.petstoremobile.utils.Resource;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Repository for handling chat-related data operations.
 */
@Singleton
public class ChatRepository extends BaseRepository {
    private final ChatApi chatApi;
    private final MessageApi messageApi;
    private final CustomerApi customerApi;

    @Inject
    public ChatRepository(ChatApi chatApi, MessageApi messageApi, CustomerApi customerApi) {
        super("ChatRepository");
        this.chatApi = chatApi;
        this.messageApi = messageApi;
        this.customerApi = customerApi;
    }

    /**
     * Retrieves all chat conversations for the current user.
     */
    public LiveData<Resource<List<ConversationDTO>>> getAllConversations() {
        return executeCall(chatApi.getAllConversations());
    }

    /**
     * Retrieves the message history for a specific conversation.
     */
    public LiveData<Resource<List<MessageDTO>>> getMessages(Long conversationId) {
        return executeCall(messageApi.getMessages(conversationId));
    }

    /**
     * Sends a plain text message to a conversation.
     */
    public LiveData<Resource<MessageDTO>> sendMessage(Long conversationId, SendMessageRequest request) {
        return executeCall(messageApi.sendMessage(conversationId, request));
    }

    /**
     * Sends a message with an attachment.
     */
    public LiveData<Resource<MessageDTO>> sendMessageWithAttachment(Long conversationId, MultipartBody.Part content, MultipartBody.Part attachment) {
        return executeCall(messageApi.sendMessageWithAttachment(conversationId, content, attachment));
    }

    /**
     * Downloads an attachment for a specific message.
     */
    public LiveData<Resource<ResponseBody>> downloadAttachment(Long messageId) {
        return executeCall(messageApi.downloadAttachment(messageId));
    }

    /**
     * Updates the status of a conversation (e.g., OPEN to CLOSED).
     */
    public LiveData<Resource<ConversationDTO>> updateConversationStatus(Long conversationId, UpdateConversationStatusRequest request) {
        return executeCall(chatApi.updateConversationStatus(conversationId, request));
    }

    /**
     * Fetches a paginated list of customers.
     */
    public LiveData<Resource<PageResponse<CustomerDTO>>> getAllCustomers(int page, int size) {
        return executeCall(customerApi.getAllCustomers(page, size));
    }
}
