package com.example.myapplication.database.entities.repository

import com.example.myapplication.database.entities.dao.UserDao
import com.example.myapplication.database.entities.model.ItemFromCart
import com.example.myapplication.database.entities.model.User
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first

class OfflineUserRepository(private val userDao: UserDao) : UserRepository {
    override suspend fun getAllUsers(): List<User> = userDao.getAll()
    override suspend fun getUserById(idUser: Int?): User? = userDao.getUserById(idUser).first()


    override suspend fun getCartByUser(userId: Int): List<ItemFromCart> =
        userDao.getCartByUid(userId)

    override suspend fun insertUser(user: User) = userDao.insert(user)

    override suspend fun updateUser(user: User) = userDao.update(user)

    override suspend fun deleteUser(user: User) = userDao.delete(user)

    suspend fun insertUsers(users: List<User>) =
        userDao.insert(*users.toTypedArray())

    suspend fun clearUsers() = userDao.deleteAll()
}