package com.syncflow.data.db

import androidx.room.*
import com.syncflow.data.db.entities.SyncFileStateEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface SyncFileStateDao {
    @Query("SELECT * FROM sync_file_states WHERE syncPairId = :pairId ORDER BY relativePath ASC")
    fun observeForPair(pairId: Long): Flow<List<SyncFileStateEntity>>

    @Query("SELECT * FROM sync_file_states WHERE syncPairId = :pairId")
    suspend fun getForPair(pairId: Long): List<SyncFileStateEntity>

    @Query("SELECT * FROM sync_file_states WHERE syncPairId = :pairId AND relativePath = :path")
    suspend fun get(pairId: Long, path: String): SyncFileStateEntity?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun upsert(entity: SyncFileStateEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun upsertAll(entities: List<SyncFileStateEntity>)

    @Query("DELETE FROM sync_file_states WHERE syncPairId = :pairId AND relativePath = :path")
    suspend fun delete(pairId: Long, path: String)

    @Query("DELETE FROM sync_file_states WHERE syncPairId = :pairId")
    suspend fun deleteForPair(pairId: Long)
}
