package com.example.orderly.ui

import androidx.lifecycle.*
import com.example.orderly.data.entity.CartItem
import com.example.orderly.data.entity.Product
import com.example.orderly.data.entity.User
import com.example.orderly.data.repository.OrderLyRepository
import kotlinx.coroutines.launch

class OrderLyViewModel(private val repository: OrderLyRepository) : ViewModel() {

    private val _allProducts = repository.allProducts
    private val _searchQuery = MutableLiveData<String>("")

    val filteredProducts: LiveData<List<Product>> = _searchQuery.switchMap { query ->
        if (query.isNullOrEmpty()) {
            _allProducts
        } else {
            _allProducts.map { products ->
                products.filter { it.name.contains(query, ignoreCase = true) }
            }
        }
    }

    val favoriteProducts: LiveData<List<Product>> = repository.favoriteProducts
    val cartItems: LiveData<List<CartItem>> = repository.cartItems
    val allUsers: LiveData<List<User>> = repository.allUsers

    val totalPrice: LiveData<Double> = cartItems.map { items ->
        items.sumOf { it.productPrice * it.quantity }
    }

    fun setSearchQuery(query: String) {
        _searchQuery.value = query
    }

    // Product Actions
    fun addProduct(product: Product) {
        viewModelScope.launch {
            repository.insertProducts(listOf(product))
        }
    }

    fun updateProduct(product: Product) {
        viewModelScope.launch {
            repository.updateProduct(product)
        }
    }

    fun deleteProduct(product: Product) {
        viewModelScope.launch {
            repository.deleteProduct(product)
        }
    }

    // Cart Actions
    fun addToCart(product: Product) {
        viewModelScope.launch {
            repository.addToCart(product)
        }
    }

    fun updateCartQuantity(cartItem: CartItem, quantity: Int) {
        viewModelScope.launch {
            repository.updateCartQuantity(cartItem, quantity)
        }
    }

    fun clearCart() {
        viewModelScope.launch {
            repository.clearCart()
        }
    }

    // User Actions
    fun registerUser(username: String, email: String, phone: String) {
        viewModelScope.launch {
            repository.insertUser(User(username = username, email = email, phone = phone))
        }
    }

    private val _loginStatus = MutableLiveData<User?>()
    val loginStatus: LiveData<User?> = _loginStatus

    fun login(email: String) {
        viewModelScope.launch {
            val user = repository.login(email)
            _loginStatus.value = user
        }
    }
}

class OrderLyViewModelFactory(private val repository: OrderLyRepository) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(OrderLyViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return OrderLyViewModel(repository) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}
