package com.example.petstoremobile.repositories;

import androidx.lifecycle.LiveData;

import com.example.petstoremobile.api.PetApi;
import com.example.petstoremobile.dtos.BulkDeleteRequest;
import com.example.petstoremobile.dtos.DropdownDTO;
import com.example.petstoremobile.dtos.PageResponse;
import com.example.petstoremobile.dtos.PetDTO;
import com.example.petstoremobile.utils.Resource;

import java.util.List;

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

import okhttp3.MultipartBody;

@Singleton
public class PetRepository extends BaseRepository {
    private final PetApi petApi;

    @Inject
    public PetRepository(PetApi petApi) {
        super("PetRepository");
        this.petApi = petApi;
    }

    /**
     * Retrieves a paginated list of pets from the API with optional filters.
     */
    public LiveData<Resource<PageResponse<PetDTO>>> getAllPets(int page, int size, String query, String status, String species, Long storeId, Long customerId, String sort) {
        return executeCall(petApi.getAllPets(page, size, query, status, species, storeId, customerId, sort));
    }

    /**
     * Retrieves a list of pets for a specific customer from the dropdowns API.
     */
    public LiveData<Resource<List<DropdownDTO>>> getCustomerPets(Long customerId) {
        return executeCall(petApi.getCustomerPets(customerId));
    }

    /**
     * Retrieves a list of pets available for adoption from the dropdowns API.
     */
    public LiveData<Resource<List<DropdownDTO>>> getAdoptionPets(Long storeId) {
        return executeCall(petApi.getAdoptionPets(storeId));
    }

    /**
     * Retrieves all pets from the dropdowns API.
     */
    public LiveData<Resource<List<DropdownDTO>>> getPetDropdowns() {
        return executeCall(petApi.getPetDropdowns());
    }

    public LiveData<Resource<List<DropdownDTO>>> getPetSpeciesDropdowns() {
        return executeCall(petApi.getPetSpeciesDropdowns());
    }

    public LiveData<Resource<List<DropdownDTO>>> getPetBreedsDropdowns(String species) {
        return executeCall(petApi.getPetBreedsDropdowns(species));
    }

    /**
     * Retrieves available pets for a specific store.
     */
    public LiveData<Resource<PageResponse<PetDTO>>> getAvailablePetsByStore(Long storeId) {
        return executeCall(petApi.getAllPets(0, 200, null, "available", null, storeId, null, "petName"));
    }

    /**
     * Retrieves a specific pet by its ID from the API.
     */
    public LiveData<Resource<PetDTO>> getPetById(Long id) {
        return executeCall(petApi.getPetById(id));
    }

    /**
     * Sends a request to the API to create a new pet record.
     */
    public LiveData<Resource<PetDTO>> createPet(PetDTO pet) {
        return executeCall(petApi.createPet(pet));
    }

    /**
     * Sends a request to the API to update an existing pet record.
     */
    public LiveData<Resource<PetDTO>> updatePet(Long id, PetDTO pet) {
        return executeCall(petApi.updatePet(id, pet));
    }

    /**
     * Sends a request to the API to delete a specific pet record.
     */
    public LiveData<Resource<Void>> deletePet(Long id) {
        return executeCall(petApi.deletePet(id));
    }

    /**
     * Sends a request to the API to delete multiple pet records.
     */
    public LiveData<Resource<Void>> bulkDeletePets(BulkDeleteRequest request) {
        return executeCall(petApi.bulkDeletePets(request));
    }

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

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