package com.example.petstoremobile.viewmodels;

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

import com.example.petstoremobile.dtos.PageResponse;
import com.example.petstoremobile.dtos.ProductDTO;
import com.example.petstoremobile.dtos.ProductSupplierDTO;
import com.example.petstoremobile.dtos.SupplierDTO;
import com.example.petstoremobile.repositories.ProductRepository;
import com.example.petstoremobile.repositories.ProductSupplierRepository;
import com.example.petstoremobile.repositories.SupplierRepository;
import com.example.petstoremobile.utils.Resource;

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

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;

@HiltViewModel
public class ProductSupplierDetailViewModel extends ViewModel {
    private final ProductSupplierRepository psRepository;
    private final ProductRepository productRepository;
    private final SupplierRepository supplierRepository;

    private boolean isEditing = false;
    private long editProductId = -1;
    private long editSupplierId = -1;

    private final MutableLiveData<List<ProductDTO>> productList = new MutableLiveData<>(new ArrayList<>());
    private final MutableLiveData<List<SupplierDTO>> supplierList = new MutableLiveData<>(new ArrayList<>());

    @Inject
    public ProductSupplierDetailViewModel(ProductSupplierRepository psRepository, ProductRepository productRepository, SupplierRepository supplierRepository) {
        this.psRepository = psRepository;
        this.productRepository = productRepository;
        this.supplierRepository = supplierRepository;
    }

    /**
     * Sets the composite key for the product-supplier relationship and enables editing mode.
     */
    public void setEditMode(long productId, long supplierId) {
        this.isEditing = true;
        this.editProductId = productId;
        this.editSupplierId = supplierId;
    }

    /**
     * Loads the product-supplier details from the repository.
     */
    public LiveData<Resource<ProductSupplierDTO>> loadProductSupplier() {
        return psRepository.getProductSupplierById(editProductId, editSupplierId);
    }

    /**
     * Checks if the fragment is in editing mode.
     */
    public boolean isEditing() { return isEditing; }

    /**
     * Returns the product ID for the relationship being edited.
     */
    public long getEditProductId() { return editProductId; }

    /**
     * Returns the supplier ID for the relationship being edited.
     */
    public long getEditSupplierId() { return editSupplierId; }

    /**
     * Loads products for the dropdown selection.
     */
    public LiveData<Resource<PageResponse<ProductDTO>>> loadProducts() {
        return productRepository.getAllProducts(null, null, 0, 200, "prodName");
    }

    /**
     * Loads suppliers for the dropdown selection.
     */
    public LiveData<Resource<PageResponse<SupplierDTO>>> loadSuppliers() {
        return supplierRepository.getAllSuppliers(0, 200, null, "supCompany");
    }

    /**
     * Saves or updates the product-supplier relationship.
     */
    public LiveData<Resource<ProductSupplierDTO>> saveProductSupplier(ProductSupplierDTO dto) {
        if (isEditing) {
            return psRepository.updateProductSupplier(editProductId, editSupplierId, dto);
        } else {
            return psRepository.createProductSupplier(dto);
        }
    }

    /**
     * Deletes the current product-supplier relationship.
     */
    public LiveData<Resource<Void>> deleteProductSupplier() {
        return psRepository.deleteProductSupplier(editProductId, editSupplierId);
    }

    /**
     * Updates the product list.
     */
    public void setProductList(List<ProductDTO> list) { productList.setValue(list); }

    /**
     * Returns the LiveData for the product list.
     */
    public LiveData<List<ProductDTO>> getProductList() { return productList; }

    /**
     * Updates the supplier list.
     */
    public void setSupplierList(List<SupplierDTO> list) { supplierList.setValue(list); }

    /**
     * Returns the LiveData for the supplier list.
     */
    public LiveData<List<SupplierDTO>> getSupplierList() { return supplierList; }
}
