package com.gestoreasy.database.dao

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

@Dao
interface OrderDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(order: Order): Long
    
    @Update
    suspend fun update(order: Order)
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertOrUpdate(order: Order)
    
    @Query("SELECT * FROM orders WHERE id = :id")
    suspend fun getById(id: Long): Order?
    
    @Query("SELECT * FROM orders ORDER BY created_at DESC")
    suspend fun getAll(): List<Order>
    
    @Query("SELECT * FROM orders WHERE synced = 0 ORDER BY created_at ASC")
    suspend fun getPendingSync(): List<Order>
    
    @Query("UPDATE orders SET synced = 1 WHERE id = :id")
    suspend fun markAsSynced(id: Long)
    
    @Query("SELECT MAX(updated_at) FROM orders")
    suspend fun getLastSyncTime(): Long?
    
    @Query("UPDATE orders SET updated_at = :timestamp")
    suspend fun updateLastSyncTime(timestamp: Long)
    
    @Query("SELECT * FROM orders ORDER BY created_at DESC")
    fun getAllFlow(): Flow<List<Order>>
}

