package com.example.petstoremobile.viewmodels;

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

import com.example.petstoremobile.dtos.ActivityLogDTO;
import com.example.petstoremobile.dtos.DropdownDTO;
import com.example.petstoremobile.repositories.ActivityLogRepository;
import com.example.petstoremobile.repositories.StoreRepository;
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 ActivityLogListViewModel extends ViewModel {
    private static final int LIMIT = 2000;

    private final ActivityLogRepository repository;
    private final StoreRepository storeRepository;

    private final MutableLiveData<List<ActivityLogDTO>> logs = new MutableLiveData<>(new ArrayList<>());
    private final MutableLiveData<List<DropdownDTO>> storeOptions = new MutableLiveData<>(new ArrayList<>());
    private final MutableLiveData<Boolean> isLoading = new MutableLiveData<>(false);

    private Long currentStoreId = null;
    private String currentRole = null;
    private String currentSearch = null;

    @Inject
    public ActivityLogListViewModel(ActivityLogRepository repository, StoreRepository storeRepository) {
        this.repository = repository;
        this.storeRepository = storeRepository;
    }

    public LiveData<List<ActivityLogDTO>> getLogs() { return logs; }
    public LiveData<List<DropdownDTO>> getStoreOptions() { return storeOptions; }
    public LiveData<Boolean> getIsLoading() { return isLoading; }

    public void loadInitialData() {
        loadStores();
        loadLogs();
    }

    private void loadStores() {
        observeOnce(storeRepository.getStoreDropdowns(), resource -> {
            if (resource != null && resource.status == Resource.Status.SUCCESS && resource.data != null) {
                storeOptions.setValue(resource.data);
            }
        });
    }

    public void loadLogs() {
        isLoading.setValue(true);
        observeOnce(repository.getActivityLogs(LIMIT, currentStoreId, currentRole, currentSearch), resource -> {
            if (resource != null) {
                if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
                    logs.setValue(resource.data);
                    isLoading.setValue(false);
                } else if (resource.status == Resource.Status.ERROR) {
                    isLoading.setValue(false);
                }
            }
        });
    }

    public void setRoleFilter(String role) {
        currentRole = "All Roles".equals(role) ? null : role;
        loadLogs();
    }

    public void setStoreFilter(Long storeId) {
        currentStoreId = storeId;
        loadLogs();
    }

    public void setSearchQuery(String query) {
        currentSearch = (query == null || query.trim().isEmpty()) ? null : query.trim();
        loadLogs();
    }

    private <T> void observeOnce(LiveData<Resource<T>> liveData, Observer<Resource<T>> handler) {
        liveData.observeForever(new Observer<Resource<T>>() {
            @Override
            public void onChanged(Resource<T> resource) {
                if (resource == null || resource.status != Resource.Status.LOADING) {
                    liveData.removeObserver(this);
                }
                handler.onChanged(resource);
            }
        });
    }
}
