package com.example.petstoremobile.viewmodels;

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

import com.example.petstoremobile.dtos.CategoryDTO;
import com.example.petstoremobile.dtos.DropdownDTO;
import com.example.petstoremobile.dtos.PageResponse;
import com.example.petstoremobile.dtos.ProductDTO;
import com.example.petstoremobile.repositories.CategoryRepository;
import com.example.petstoremobile.repositories.ProductRepository;
import com.example.petstoremobile.utils.Resource;

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

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;
import okhttp3.MultipartBody;

@HiltViewModel
public class ProductDetailViewModel extends ViewModel {
    private final ProductRepository productRepository;
    private final CategoryRepository categoryRepository;

    private final MutableLiveData<List<DropdownDTO>> categoryList = new MutableLiveData<>(new ArrayList<>());
    private long prodId = -1;
    private boolean isEditing = false;

    @Inject
    public ProductDetailViewModel(ProductRepository productRepository, CategoryRepository categoryRepository) {
        this.productRepository = productRepository;
        this.categoryRepository = categoryRepository;
    }

    /**
     * Sets the product ID and initializes the editing mode.
     */
    public void setProdId(long id) {
        this.prodId = id;
        this.isEditing = id != -1;
    }

    /**
     * Returns the current product ID.
     */
    public long getProdId() {
        return prodId;
    }

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

    /**
     * Loads product category dropdown options.
     */
    public LiveData<Resource<List<DropdownDTO>>> loadCategories() {
        return productRepository.getCategoryDropdowns();
    }

    /**
     * Loads the product details from the repository.
     */
    public LiveData<Resource<ProductDTO>> loadProduct() {
        return productRepository.getProductById(prodId);
    }

    /**
     * Saves or updates the product record.
     */
    public LiveData<Resource<ProductDTO>> saveProduct(ProductDTO dto) {
        if (isEditing) {
            return productRepository.updateProduct(prodId, dto);
        } else {
            return productRepository.createProduct(dto);
        }
    }

    /**
     * Deletes the current product record.
     */
    public LiveData<Resource<Void>> deleteProduct() {
        return productRepository.deleteProduct(prodId);
    }

    /**
     * Uploads an image for the current product.
     */
    public LiveData<Resource<Void>> uploadProductImage(MultipartBody.Part image) {
        return productRepository.uploadProductImage(prodId, image);
    }

    /**
     * Deletes the current product's image.
     */
    public LiveData<Resource<Void>> deleteProductImage() {
        return productRepository.deleteProductImage(prodId);
    }

    /**
     * Updates the category list.
     */
    public void setCategoryList(List<DropdownDTO> list) {
        categoryList.setValue(list);
    }

    /**
     * Returns the LiveData for the category list.
     */
    public LiveData<List<DropdownDTO>> getCategoryList() {
        return categoryList;
    }
}

