package com.example.petstoremobile.repositories;

import androidx.lifecycle.LiveData;

import com.example.petstoremobile.api.ProductApi;
import com.example.petstoremobile.dtos.DropdownDTO;
import com.example.petstoremobile.dtos.PageResponse;
import com.example.petstoremobile.dtos.ProductDTO;
import com.example.petstoremobile.utils.Resource;

import java.util.List;

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

import okhttp3.MultipartBody;

@Singleton
public class ProductRepository extends BaseRepository {
    private final ProductApi productApi;

    @Inject
    public ProductRepository(ProductApi productApi) {
        super("ProductRepository");
        this.productApi = productApi;
    }

    /**
     * Retrieves a paginated list of products from the API, filtered by an optional query, category and sorted.
     */
    public LiveData<Resource<PageResponse<ProductDTO>>> getAllProducts(String query, Long categoryId, int page, int size, String sort) {
        return executeCall(productApi.getAllProducts(query, categoryId, page, size, sort));
    }

    /**
     * Retrieves a specific product by its ID from the API.
     */
    public LiveData<Resource<ProductDTO>> getProductById(Long id) {
        return executeCall(productApi.getProductById(id));
    }

    /**
     * Sends a request to the API to create a new product.
     */
    public LiveData<Resource<ProductDTO>> createProduct(ProductDTO product) {
        return executeCall(productApi.createProduct(product));
    }

    /**
     * Sends a request to the API to update an existing product by ID.
     */
    public LiveData<Resource<ProductDTO>> updateProduct(Long id, ProductDTO product) {
        return executeCall(productApi.updateProduct(id, product));
    }

    /**
     * Sends a request to the API to delete a specific product.
     */
    public LiveData<Resource<Void>> deleteProduct(Long id) {
        return executeCall(productApi.deleteProduct(id));
    }

    /**
     * Uploads an image file for a specific product via the API.
     */
    public LiveData<Resource<Void>> uploadProductImage(Long id, MultipartBody.Part image) {
        return executeCall(productApi.uploadProductImage(id, image));
    }

    /**
     * Sends a request to the API to delete the image of a specific product.
     */
    public LiveData<Resource<Void>> deleteProductImage(Long id) {
        return executeCall(productApi.deleteProductImage(id));
    }

    /**
     * Retrieves a list of product dropdowns from the API.
     */
    public LiveData<Resource<List<DropdownDTO>>> getProductDropdowns() {
        return executeCall(productApi.getProductDropdowns());
    }

    /**
     * Retrieves a list of category dropdowns from the API.
     */
    public LiveData<Resource<List<DropdownDTO>>> getCategoryDropdowns() {
        return executeCall(productApi.getCategoryDropdowns());
    }
}
