package com.example.petstoremobile.viewmodels;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.petstoremobile.dtos.BulkDeleteRequest;
import com.example.petstoremobile.dtos.SupplierDTO;
import com.example.petstoremobile.repositories.SupplierRepository;
import com.example.petstoremobile.utils.Resource;

import androidx.lifecycle.Observer;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;

@HiltViewModel
public class SupplierListViewModel extends ViewModel {
    private final SupplierRepository repository;

    private final MutableLiveData<List<SupplierDTO>> suppliers = new MutableLiveData<>(new ArrayList<>());
    private final MutableLiveData<Boolean> isLoading = new MutableLiveData<>(false);

    private int currentPage = 0;
    private boolean isLastPage = false;
    private static final int PAGE_SIZE = 20;

    @Inject
    public SupplierListViewModel(SupplierRepository repository) {
        this.repository = repository;
    }

    /**
     * Returns the LiveData for the list of suppliers.
     */
    public LiveData<List<SupplierDTO>> getSuppliers() { return suppliers; }

    /**
     * Returns the LiveData for the loading state.
     */
    public LiveData<Boolean> getIsLoading() { return isLoading; }

    /**
     * Checks if the last page of suppliers has been reached.
     */
    public boolean isLastPage() { return isLastPage; }

    /**
     * Loads suppliers from the repository with the specified filters.
     */
    public void loadSuppliers(boolean reset, String query) {
        if (isLoading.getValue() != null && isLoading.getValue() && !reset) return;

        if (reset) {
            currentPage = 0;
            isLastPage = false;
        }

        if (isLastPage) return;

        isLoading.setValue(true);
        observeOnce(repository.getAllSuppliers(currentPage, PAGE_SIZE, query, "supCompany"), resource -> {
            if (resource != null) {
                if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
                    List<SupplierDTO> currentList = reset ? new ArrayList<>() : new ArrayList<>(suppliers.getValue());
                    currentList.addAll(resource.data.getContent());
                    suppliers.setValue(currentList);
                    isLastPage = resource.data.isLast();
                    if (!isLastPage) currentPage++;
                    isLoading.setValue(false);
                } else if (resource.status == Resource.Status.ERROR) {
                    isLoading.setValue(false);
                }
            }
        });
    }

    private <T> void observeOnce(LiveData<Resource<T>> liveData, Observer<Resource<T>> handler) {
        liveData.observeForever(new Observer<Resource<T>>() {
            @Override
            public void onChanged(Resource<T> resource) {
                if (resource == null || resource.status != Resource.Status.LOADING) {
                    liveData.removeObserver(this);
                }
                handler.onChanged(resource);
            }
        });
    }

    /**
     * Deletes multiple suppliers by their IDs.
     */
    public LiveData<Resource<Void>> bulkDeleteSuppliers(List<String> ids) {
        List<Long> longIds = ids.stream().map(Long::valueOf).collect(java.util.stream.Collectors.toList());
        return repository.bulkDeleteSuppliers(new BulkDeleteRequest(longIds));
    }
}
