package com.gestoreasy.database.dao

import androidx.room.*
import com.gestoreasy.database.entities.Product
import kotlinx.coroutines.flow.Flow

@Dao
interface ProductDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(product: Product): Long
    
    @Update
    suspend fun update(product: Product)
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertOrUpdate(product: Product)
    
    @Query("SELECT * FROM products WHERE id = :id")
    suspend fun getById(id: Long): Product?
    
    @Query("SELECT * FROM products ORDER BY nome ASC")
    suspend fun getAll(): List<Product>
    
    @Query("SELECT * FROM products WHERE ativo = 1 ORDER BY nome ASC")
    suspend fun getActive(): List<Product>
    
    @Query("SELECT * FROM products WHERE nome LIKE :query OR codigo LIKE :query ORDER BY nome ASC")
    suspend fun search(query: String): List<Product>
    
    @Query("SELECT MAX(last_sync) FROM products")
    suspend fun getLastSyncTime(): Long?
    
    @Query("UPDATE products SET last_sync = :timestamp")
    suspend fun updateLastSyncTime(timestamp: Long)
    
    @Query("SELECT * FROM products WHERE ativo = 1 ORDER BY nome ASC")
    fun getAllFlow(): Flow<List<Product>>
}

