package com.example.bnsptest.data.repository

import com.example.bnsptest.data.local.dao.UserDao
import com.example.bnsptest.data.local.entity.User
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class UserRepository(private val userDao: UserDao) {

    suspend fun insertInitialUser() {
        withContext(Dispatchers.IO) {
            if (userDao.getUserCount() == 0) {
                val users = listOf(
                    User("admin@example.com", "Admin", "admin123"),
                    User("johndoe@example.com", "John Doe", "johndoe"),
                    User("jackripper@example.com", "Jack Ripper", "jackripper"),
                    User("eddieaderson@example.com", "Eddie Aderson", "eddieaderson"),
                    User("victoria@example.com", "Victoria", "victoria"),
                    User("jake@example.com", "Jake", "jake"),
                    User("Lucy@example.com", "Lucy", "lucy")
                )
                users.forEach { userDao.insertUser(it) }
            }
        }
    }

    suspend fun login(email: String, password: String): User? {
        return withContext(Dispatchers.IO) {
            val user = userDao.getUserByEmail(email)
            if (user?.password == password) user else null
        }
    }
}
