package com.example.myapplication.database.room.repository.offline

import com.example.myapplication.database.room.dao.user.UserDao
import com.example.myapplication.database.room.model.item.ItemFromCart
import com.example.myapplication.database.room.model.user.User
import com.example.myapplication.database.room.repository.online.UserRepository
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()
}