package pl.firmatpp.kierowca.data.sync

import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey

@Entity(tableName = "driver_bootstrap_cache")
data class DriverBootstrapCacheEntity(
    @PrimaryKey val date: String,
    val payloadJson: String,
    val checksum: String?,
    val version: Long?,
    val syncedAtEpochMillis: Long,
)

@Entity(tableName = "driver_route_cache")
data class DriverRouteCacheEntity(
    @PrimaryKey val routeId: String,
    val payloadJson: String,
    val checksum: String?,
    val version: Long?,
    val syncedAtEpochMillis: Long,
)

@Entity(
    tableName = "driver_sync_states",
    indices = [
        Index(value = ["scope", "date", "routeId"], unique = true),
    ],
)
data class DriverSyncStateEntity(
    @PrimaryKey val key: String,
    val scope: String,
    val date: String?,
    val routeId: String?,
    val checksum: String,
    val version: Long,
    val computedAt: String,
    val syncedAtEpochMillis: Long,
)

data class CachedValue<T>(
    val value: T,
    val stale: Boolean,
    val syncedAtEpochMillis: Long?,
)

fun syncStateKey(scope: String, date: String?, routeId: String?): String =
    listOf(scope, date ?: "-", routeId ?: "-").joinToString(":")
