package com.divya.hardinfo.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.divya.hardinfo.data.HardwareRepository
import com.divya.hardinfo.database.HardInfoDatabase
import com.divya.hardinfo.model.Category
import com.divya.hardinfo.model.HardwareItem
import kotlinx.coroutines.launch

class HardwareViewModel @JvmOverloads constructor(
    application: Application, 
    private val repository: HardwareRepository = HardwareRepository(application, HardInfoDatabase.getDatabase(application).favoriteDao(), HardInfoDatabase.getDatabase(application).searchHistoryDao())
) : AndroidViewModel(application) {
    
    // LiveData for Categories
    private val _categories = MutableLiveData<List<Category>>()
    val categories: LiveData<List<Category>> = _categories

    // LiveData for Items in a Category
    private val _categoryItems = MutableLiveData<List<HardwareItem>>()
    val categoryItems: LiveData<List<HardwareItem>> = _categoryItems

    // LiveData for Selected Item Details
    private val _selectedItem = MutableLiveData<HardwareItem?>()
    val selectedItem: LiveData<HardwareItem?> = _selectedItem

    // LiveData for Favorites
    private val _favorites = MutableLiveData<List<HardwareItem>>()
    val favorites: LiveData<List<HardwareItem>> = _favorites
    
     // LiveData for Search Results
    private val _searchResults = MutableLiveData<List<HardwareItem>>()
    val searchResults: LiveData<List<HardwareItem>> = _searchResults


    init {
        loadCategories()
    }

    fun loadCategories() {
        viewModelScope.launch {
            _categories.value = repository.getCategories()
        }
    }

    fun loadItemsByCategory(categoryName: String) {
         viewModelScope.launch {
             // Ensure data is loaded
             val cats = repository.getCategories() 
             val cat = cats.find { it.name == categoryName }
             _categoryItems.value = cat?.items ?: emptyList()
         }
    }
    
    fun loadItemById(itemId: String) {
        viewModelScope.launch {
             _selectedItem.value = repository.getItemById(itemId)
        }
    }

    fun toggleFavorite(item: HardwareItem) {
        viewModelScope.launch {
            repository.toggleFavorite(item)
            // Refresh favorites list if actively viewing it, or just update state
            loadFavorites()
            // Also need to reflect change in current list if visible
            _categoryItems.value?.let { list ->
                list.find { it.id == item.id }?.isFavorite = item.isFavorite
                _categoryItems.postValue(list)
            }
            
             if (_selectedItem.value?.id == item.id) {
                _selectedItem.value = item
            }
        }
    }

    fun loadFavorites() {
        viewModelScope.launch {
            _favorites.value = repository.getFavoriteItems()
        }
    }
    
    fun search(query: String) {
        viewModelScope.launch {
            _searchResults.value = repository.searchItems(query)
            if (query.isNotBlank()) {
                repository.addSearchHistory(query)
            }
        }
    }
    
    fun getSearchHistory() = repository.getSearchHistory()
    
    fun clearSearchHistory() {
        viewModelScope.launch {
            repository.clearSearchHistory()
        }
    }
}
