package com.example.modifier.data

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update

@Dao
interface BackupItemDao {

    @Insert
    suspend fun insert(backupItem: BackupItem): Long

    @Update
    suspend fun update(backupItem: BackupItem)

    @Delete
    suspend fun delete(backupItem: BackupItem)

    @Query("SELECT * from backupitem WHERE id = :id")
    suspend fun getItem(id: Int): BackupItem?

    @Query("SELECT * FROM backupitem ORDER BY id")
    suspend fun getAll(): List<BackupItem>

    @Query("SELECT * FROM backupitem WHERE country = :country AND number = :number limit 1")
    suspend fun findBackupForNumber(country: String, number: String): BackupItem?

    @Query("SELECT * FROM backupitem WHERE number != :number AND (fresh = 1 OR lastUse < :time) order by createdAt limit 1")
    suspend fun findBackupForRestore(number: String, time: Long): BackupItem?
}