package pl.firmatpp.kierowca.data.sync

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query

@Dao
interface DriverCacheDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun upsertBootstrap(cache: DriverBootstrapCacheEntity)

    @Query("SELECT * FROM driver_bootstrap_cache WHERE date = :date LIMIT 1")
    suspend fun bootstrap(date: String): DriverBootstrapCacheEntity?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun upsertRoute(cache: DriverRouteCacheEntity)

    @Query("SELECT * FROM driver_route_cache WHERE routeId = :routeId LIMIT 1")
    suspend fun route(routeId: String): DriverRouteCacheEntity?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun upsertSyncState(state: DriverSyncStateEntity)

    @Query("SELECT * FROM driver_sync_states WHERE scope = :scope AND ifnull(date, '-') = ifnull(:date, '-') AND ifnull(routeId, '-') = ifnull(:routeId, '-') LIMIT 1")
    suspend fun syncState(scope: String, date: String?, routeId: String?): DriverSyncStateEntity?

    @Query("DELETE FROM driver_bootstrap_cache")
    suspend fun clearBootstrap()

    @Query("DELETE FROM driver_route_cache")
    suspend fun clearRoutes()

    @Query("DELETE FROM driver_sync_states")
    suspend fun clearSyncStates()
}
