package com.gestoreasy.database.dao

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

@Dao
interface CommissionDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertOrUpdate(commission: Commission)
    
    @Query("SELECT * FROM commissions WHERE vendedor_id = :vendedorId ORDER BY data_calculo DESC")
    suspend fun getByVendedor(vendedorId: Long): List<Commission>
    
    @Query("SELECT * FROM commissions WHERE vendedor_id = :vendedorId ORDER BY data_calculo DESC")
    fun getByVendedorFlow(vendedorId: Long): Flow<List<Commission>>
    
    @Query("SELECT * FROM commissions WHERE data_calculo >= :startDate AND data_calculo <= :endDate ORDER BY data_calculo DESC")
    suspend fun getByPeriod(startDate: Long, endDate: Long): List<Commission>
    
    @Query("SELECT COALESCE(SUM(valor_comissao), 0) FROM commissions WHERE vendedor_id = :vendedorId AND status = 'paga'")
    suspend fun getTotalByVendedor(vendedorId: Long): Double
}

