package com.gestoreasy.repository

import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.gestoreasy.MobileApp
import com.gestoreasy.database.AppDatabase
import com.gestoreasy.database.entities.Product
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*

@RunWith(AndroidJUnit4::class)
class ProductRepositoryTest {
    private lateinit var database: AppDatabase
    private lateinit var repository: ProductRepository
    
    @Before
    fun setup() {
        database = Room.inMemoryDatabaseBuilder(
            ApplicationProvider.getApplicationContext(),
            AppDatabase::class.java
        ).allowMainThreadQueries().build()
        
        repository = ProductRepository(database.productDao())
    }
    
    @After
    fun tearDown() {
        database.close()
    }
    
    @Test
    fun testInsertAndGetProduct() = runBlocking {
        val product = Product(
            id = 1,
            codigo = "PROD001",
            nome = "Produto Teste",
            descricao = "Descrição do produto",
            preco = 99.99,
            estoque = 10.0,
            unidade = "UN",
            categoria = "Categoria 1",
            ativo = true
        )
        
        repository.insert(product)
        val retrieved = repository.getById(1)
        
        assertNotNull(retrieved)
        assertEquals("Produto Teste", retrieved?.nome)
        assertEquals(99.99, retrieved?.preco, 0.01)
    }
    
    @Test
    fun testGetAllProducts() = runBlocking {
        val product1 = Product(
            id = 1,
            codigo = "PROD001",
            nome = "Produto 1",
            preco = 10.0,
            estoque = 5.0
        )
        val product2 = Product(
            id = 2,
            codigo = "PROD002",
            nome = "Produto 2",
            preco = 20.0,
            estoque = 10.0
        )
        
        repository.insert(product1)
        repository.insert(product2)
        
        val all = repository.getAll()
        assertEquals(2, all.size)
    }
}

