package com.example.kasirapp.data

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction as RoomTransaction // Alias to avoid conflict with our Transaction class

@Dao
interface TransactionDao {
    @Insert
    suspend fun insertTransaction(transaction: Transaction): Long // Returns the new transaction's ID

    @Insert
    suspend fun insertTransactionItems(items: List<TransactionItem>)

    @RoomTransaction
    @Query("SELECT * FROM transactions ORDER BY timestamp DESC")
    suspend fun getTransactionsWithItems(): List<TransactionWithItems>
    
    @RoomTransaction
    @Query("SELECT * FROM transactions WHERE id = :id")
    suspend fun getTransactionWithItemsById(id: Long): TransactionWithItems?

    @RoomTransaction
    @Query("SELECT * FROM transactions WHERE timestamp BETWEEN :startDate AND :endDate ORDER BY timestamp DESC")
    suspend fun getTransactionsWithItemsInRange(startDate: Long, endDate: Long): List<TransactionWithItems>

    // We keep this function in case we need to get just the transaction objects
    @Query("SELECT * FROM transactions ORDER BY timestamp DESC")
    suspend fun getAllTransactions(): List<Transaction>
}
