package com.example.cuida.ui.appointments;

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.AppointmentDao;
import com.example.cuida.data.model.Appointment;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AppointmentsViewModel extends AndroidViewModel {

    private final AppointmentDao appointmentDao;
    private final LiveData<List<Appointment>> futureAppointments;
    private final LiveData<List<Appointment>> pastAppointments;
    private final ExecutorService executorService;

    public AppointmentsViewModel(@NonNull Application application) {
        super(application);
        AppDatabase db = AppDatabase.getDatabase(application);
        appointmentDao = db.appointmentDao();
        futureAppointments = appointmentDao.getFutureAppointments();
        pastAppointments = appointmentDao.getPastAppointments();
        executorService = Executors.newSingleThreadExecutor();
    }

    public LiveData<List<Appointment>> getFutureAppointments() {
        return futureAppointments;
    }

    public LiveData<List<Appointment>> getPastAppointments() {
        return pastAppointments;
    }

    public void insert(Appointment appointment) {
        executorService.execute(() -> appointmentDao.insert(appointment));
    }
}
