package com.example.cuida.ui.medication;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.example.cuida.data.AppDatabase;
import com.example.cuida.data.dao.MedicationDao;
import com.example.cuida.data.model.Medication;
import java.util.List;

public class MedicationViewModel extends AndroidViewModel {

    private final LiveData<List<Medication>> allMedications;
    private final LiveData<Medication> nextMedication;

    public MedicationViewModel(@NonNull Application application) {
        super(application);
        AppDatabase db = AppDatabase.getDatabase(application);
        MedicationDao medicationDao = db.medicationDao();
        allMedications = medicationDao.getAllMedications();
        nextMedication = medicationDao.getNextMedication();
    }

    public LiveData<List<Medication>> getAllMedications() {
        return allMedications;
    }

    public LiveData<Medication> getNextMedication() {
        return nextMedication;
    }

    public void update(Medication medication) {
        AppDatabase.databaseWriteExecutor.execute(() -> {
            AppDatabase.getDatabase(getApplication()).medicationDao().update(medication);
        });
    }
}
