package com.example.petstoremobile.viewmodels;

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

import com.example.petstoremobile.dtos.CouponDTO;
import com.example.petstoremobile.repositories.CouponRepository;
import com.example.petstoremobile.utils.Resource;

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;

@HiltViewModel
public class CouponDetailViewModel extends ViewModel {
    private final CouponRepository repository;
    private long couponId = -1;
    private boolean isEditing = false;

    @Inject
    public CouponDetailViewModel(CouponRepository repository) {
        this.repository = repository;
    }

    public LiveData<Resource<CouponDTO>> loadCoupon(long id) {
        return repository.getCouponById(id);
    }

    public void setCouponId(long id, boolean isEditing) {
        this.couponId = id;
        this.isEditing = isEditing;
    }

    public long getCouponId() {
        return couponId;
    }

    public boolean isEditing() {
        return isEditing;
    }

    public LiveData<Resource<CouponDTO>> saveCoupon(CouponDTO dto) {
        if (isEditing && couponId > 0) {
            return repository.updateCoupon(couponId, dto);
        } else {
            return repository.createCoupon(dto);
        }
    }

    public LiveData<Resource<Void>> deleteCoupon() {
        return repository.deleteCoupon(couponId);
    }
}
